python / twisted / exec environment
Does Python Twisted pass the parent environment to child processes?
By default no, but if you pass env=None
then it does. Ergo,
default is env={}
.
Let’s build a quick example. For those unfamiliar with twisted this may provide a quick intro.
import os
from twisted.internet import protocol, reactor, utils
This is what we’re going to “run”:
proc = ['/bin/sh', '-c', 'export']
#kwargs = {'env': None}
kwargs = {}
A quick way to show output and end after the 2 runs.
done = 0
def on_output(data):
global done
print data
done += 1
if done == 2:
reactor.stop()
Spawning a process, the regular way.
class MyProcessProtocol(protocol.ProcessProtocol):
def outReceived(self, data):
on_output(data)
reactor.spawnProcess(MyProcessProtocol(), proc[0], proc, **kwargs)
Spawning a process, the easier way. Observe that you’re not supposed to
pass argv[0]
explicitly here. Not very intuitive…
deferred = utils.getProcessOutput(proc[0], proc[1:], **kwargs)
deferred.addCallback(on_output)
Lastly, fire it up.
reactor.run()