Last active
May 22, 2018 12:52
-
-
Save justinfx/b94fab9d1f056380cb28 to your computer and use it in GitHub Desktop.
Using subprocess in Python, to communicate between Parent/Child via STDIN pipe (https://groups.google.com/d/msg/python_inside_maya/waJHs-gsS4c/j0_q39Nj4h0J)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/local/bin python | |
| import time | |
| import subprocess | |
| def main(): | |
| p = subprocess.Popen(["python", "child.py"], bufsize=0, stdin=subprocess.PIPE) | |
| for i in xrange(10): | |
| p.stdin.write("Parent command: %d\n" % i) | |
| time.sleep(1) | |
| p.terminate() | |
| if __name__ == '__main__': | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/local/bin python | |
| import os | |
| import sys | |
| import time | |
| import threading | |
| def watch(): | |
| t = threading.Thread(target=_watch) | |
| t.daemon = True | |
| t.start() | |
| def _watch(): | |
| print "[STDOUT] Watching for stdin..." | |
| for line in iter(sys.stdin.readline, None): | |
| print "[STDIN ]", line.strip() | |
| def main(): | |
| print "[STDOUT] Child pid:", os.getpid() | |
| watch() | |
| while True: | |
| print "[STDOUT] Child sleeping..." | |
| time.sleep(1) | |
| print "[STDOUT] Child exiting" | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
your file names in the gist are switched around
parent.py <--> child.py