Download Presentation Slides

Transcript
Some Sample Code
• A sample child process
# child.py
import channel
import sys
ch = channel.Channel(sys.stdout,sys.stdin)
while True:
item = ch.recv()
ch.send(("child",item))
• Parent process setup
# parent.py
import channel
import subprocess
p = subprocess.Popen(['python','child.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
ch = channel.Channel(p.stdin,p.stdout)
Copyright (C) 2009, David Beazley, http://www.dabeaz.com
123
Some Sample Code
• Using the child worker
>>> ch.send("Hello World")
Hello World
This output is being
>>> ch.send(42)
produced
by the child
42
>>> ch.send([1,2,3,4])
[1, 2, 3, 4]
>>> ch.send({'host':'python.org','port':80})
{'host': 'python.org', 'port': 80}
>>>
• You can send almost any Python object
(numbers, lists, dictionaries, instances, etc.)
Copyright (C) 2009, David Beazley, http://www.dabeaz.com
124