I have a simple example:
subprocesses = {}
class MyPP(protocol.ProcessProtocol):
def processExited(self, reason):
print "processExited, status %s" % (reason.value.exitCode,)
class Test:
def run(self):
for i in range(0, max_processes):
pp = MyPP()
command = ['sleep','10']
subprocess = reactor.spawnProcess(pp, command[0], command, {})
subprocesses[subprocess.pid] = subprocess
reactor.run()
Test().run()
I want to delete from dictionary subprocesses element then subprocess is exited. How to do it ?
subprocesses = {}
max_processes = 3
from twisted.internet import protocol, reactor
class MyPP(protocol.ProcessProtocol):
def connectionMade(self):
self.pid = self.transport.pid
def processExited(self, reason):
print "processExited, status %s" % (reason.value.exitCode,)
del subprocesses[self.pid]
print 'Remaining subprocesses', subprocesses
class Test:
def run(self):
for i in range(0, max_processes):
pp = MyPP()
command = ['sleep','3']
subprocess = reactor.spawnProcess(pp, command[0], command, {})
subprocesses[subprocess.pid] = subprocess
Test().run()
reactor.run()
Notice a couple things:
transport.pid
is no longer valid by the time processExited
is called, so if you need to use it after the process exits you need to save it earlier. This is what happens in the connectionMade
method.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With