Skip to content

Instantly share code, notes, and snippets.

@saghul
Created October 4, 2012 06:23
Show Gist options
  • Save saghul/3831767 to your computer and use it in GitHub Desktop.
Save saghul/3831767 to your computer and use it in GitHub Desktop.
pyuv child process example
#!/usr/bin/env python
import os
import pyuv
import sys
fd = os.getenv('PYUV_CHANNEL_FD')
if fd is None:
print "[Child] No channel fd found"
sys.exit(1)
def read_cb(handle, data, error):
print "[Child] Received data: %s" % data
if data and data.strip() == 'PONG':
handle.close()
loop = pyuv.Loop.default_loop()
channel = pyuv.Pipe(loop, True)
channel.open(int(fd))
channel.start_read(read_cb)
channel.write('PING')
loop.run()
import os
import pyuv
import sys
def proc_exit_cb(proc, exit_status, term_signal):
print "Process exited with status %d and term signal %d" % (exit_status, term_signal)
proc.close()
def read_cb(handle, data, error):
print "Received data: %s" % data
if data and data.strip() == 'PING':
handle.write('PONG')
loop = pyuv.Loop.default_loop()
channel = pyuv.Pipe(loop, True)
stdio = []
stdio.append(pyuv.StdIO(fd=sys.stdin.fileno(), flags=pyuv.UV_INHERIT_FD))
stdio.append(pyuv.StdIO(fd=sys.stdout.fileno(), flags=pyuv.UV_INHERIT_FD))
stdio.append(pyuv.StdIO(fd=sys.stderr.fileno(), flags=pyuv.UV_INHERIT_FD))
#stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE))
#stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE))
#stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE))
stdio.append(pyuv.StdIO(stream=channel, flags=pyuv.UV_CREATE_PIPE|pyuv.UV_READABLE_PIPE|pyuv.UV_WRITABLE_PIPE))
proc = pyuv.Process(loop)
env = os.environ.copy()
env['PYUV_CHANNEL_FD'] = '3'
proc.spawn(file="./test_child.py", exit_callback=proc_exit_cb, stdio=stdio, env=env)
channel.start_read(read_cb)
loop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment