'When using python to run adb shell command, how can i kill a running program in adb shell?

For Android device, if i enter adb shell in iTerm and then run some program to continuously output data(like getevent, logcat), i can kill these programs by CTRL+C and return to adb shell(not return to the OS shell).

What i want to know is how can i do the same thing in python?

here's my sample code:

def thread_func(process):
    while True:
        out = process.stdout.readline()
        line = out.decode()
        if not line:
            print("EOF")
            break
        print(line.replace('\n', ''))

def thread_read(process):
    t = threading.Thread(target=thread_func, args=[process,])
    t.start()


process = subprocess.Popen("adb shell", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
thread_read(process)

time.sleep(1)
cmd = "ls"
print("\n=====> send " + cmd)
process.stdin.write((cmd + "\n").encode())
process.stdin.flush()

time.sleep(1)
cmd = "getevent"
print("\n=====> send " + cmd)
process.stdin.write((cmd + "\n").encode())
process.stdin.flush()

# after sending 'getevent' i can continuously read event data from stdout
# but, how can i kill the getevent and back to adb shell environment so that i can send a new command?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source