Source

green380 / examples / child_proc.py

Full commit
#!/usr/bin/env python3.3

from fcntl import fcntl, F_SETFL
from green380 import spawn, run
from os import O_NONBLOCK
from pdb import set_trace
from signal import SIGCHLD
from subprocess import Popen, PIPE
import sys

def find_var():
    proc = Popen(['find', '/var'], stdout=PIPE, stderr=PIPE)
    lines = 0
    @spawn
    def sigchld():
        while proc.poll() is None:
            yield 'signal', SIGCHLD
        print('process returncode:', proc.returncode)
    @spawn
    def read_stdout():
        nonlocal lines
        while True:
            yield 'read', proc.stdout
            buf = proc.stdout.read(0x1000)
            if not buf:
                break
            lines += buf.count(b'\n')
        print(lines, 'lines')
    @spawn
    def read_stderr():
        while True:
            yield 'read', proc.stderr
            line = proc.stderr.readline()
            if not line:
                break
            sys.stderr.buffer.raw.write(line)

find_var()
run()