我想知道当父进程试图终止子进程时是否有办法在子进程上运行一些代码。有没有办法我们可以写一个Exception
也许?
我的代码看起来像这样:
main_process.py
import Process from multiprocessing
def main():
p1 = Process(target = child, args = (arg1, ))
p1.start()
p1.daemon = True
#blah blah blah code here
sleep(5)
p1.terminate()
def child(arg1):
#blah blah blah
itemToSend = {}
#more blah blah
snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish
try:
snmpEngine.transportDispatcher.runDispatcher()
except:
snmpEngine.transportDispatcher.closeDispatcher()
raise
由于作业永远不会完成,因此子进程会继续运行。我必须从父进程终止它,因为子进程永远不会自行终止。但是,我想itemToSend
在子进程终止之前发送给父进程。我return
可以以某种方式对父进程进行处理吗?
更新:让我解释一下模块runDispatcher()
的工作原理pysnmp
def runDispatcher():
while jobsArePending(): # jobs are always pending because of jobStarted() function
loop()
def jobStarted(jobId):
if jobId in jobs: #This way there's always 1 job remaining
jobs[jobId] = jobs[jobId] + 1
这非常令人沮丧。除了做这一切,是否可以自己编写一个 snmp 陷阱侦听器?你能指出我正确的资源吗?