Skip to content

Instantly share code, notes, and snippets.

@justinfx
Last active May 22, 2018 12:52
Show Gist options
  • Select an option

  • Save justinfx/b94fab9d1f056380cb28 to your computer and use it in GitHub Desktop.

Select an option

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)
#!/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()
#!/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()
@anentropic
Copy link

your file names in the gist are switched around parent.py <--> child.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment