从正在运行的进程中调用函数 [英] Call a function from a running process
本文介绍了从正在运行的进程中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的程序启动一个子进程,该子进程必须在初始化后向父级发送某种信号.如果我可以在父级中设置处理程序,这将是完美的,在发送此信号时会调用该处理程序.有什么办法吗?
my programm starts a subprocess, which has to send some kind of signal to the parent after initialization. It would be perfekt if i could set up a handler in parent, which is called when this signal is sent. Is there any way to do it?
Alendit
推荐答案
如果使用的是Python 2.6,则可以使用标准库中的multiprocessing
模块,尤其是管道和队列.来自文档的简单示例:
If you are using Python 2.6, you can use the multiprocessing
module from the standard library, in particular pipes and queues. Simple example from the docs:
from multiprocessing import Process, Pipe
def f(conn): #This code will be spawned as a new child process
conn.send([42, None, 'hello']) #The child process sends a msg to the pipe
conn.close()
if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,)) # prepare to spawn the child
p.start() # spawn it
print parent_conn.recv() # prints "[42, None, 'hello']"
p.join() #wait for child to exit
如果您使用的是Python 2.4或2.5,请不要失望-可以使用反向端口此处.
If you are using Python 2.4 or 2.5, don't despair - a backport is available here.
这篇关于从正在运行的进程中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文