I'm a little unsure why you would want to do this. Can I suggest an entirely pythonic approach using threading and queues:
import threading from queue import Queue import time def worker(q): """thread worker function""" running = True while running: message = q.get() print('Worker received message: {}'.format(message)) if message == 'KILL': running = False print('Worker completed') if __name__ == '__main__': q = Queue() worker = threading.Thread(target=worker, args=(q,)) worker.start() running = True while running: user_input = input('Input some data: ') q.put(user_input) if user_input == 'KILL': running = False time.sleep(0.5) print('Program terminated')
You now have the ability to send commands to another python script from a console. The worker can do whatever you wish now. For a more detailed example see this post
Alternatively if thats not acceptable, what you are trying to do is 'PIPE' data from a command line in both directions. Have a look at this post where they do:
proc = subprocess.Popen(['some_cmd', '--command_line_parameters'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate()