主进程的多处理回调 - Python
有没有办法使用多处理从创建的进程回调主进程? 假设我有 main.py,它使用如下所示的多重处理创建三个进程
from multiprocessing import Process
from child import createNewProcess
def callbackFunction(data):
print(data)
if __name__ == "__main__"
process = []
for i in range(3):
p = Process(target=createNewProcess, args=(callback,))
process.append(p)
[x.start() for x in process]
,而 child.py 看起来像
def createNewProcess(externalCB):
# do something and call external callback
data = 10 + 12
externalCB(data)
我想调用callbackFunction(),它在创建的进程的主进程上下文中可用。 但在上面的情况下,它在自己的进程中创建了三个新的callbackFunction() 对象,并调用自己上下文中的对象。
如何使用多处理进程调用主callbackFunction()上下文?
is there any way to make a callback to main process from the created process using multiprocessing ?
Say I have main.py which creates three processes using multiprocessing like below
from multiprocessing import Process
from child import createNewProcess
def callbackFunction(data):
print(data)
if __name__ == "__main__"
process = []
for i in range(3):
p = Process(target=createNewProcess, args=(callback,))
process.append(p)
[x.start() for x in process]
And child.py looks like
def createNewProcess(externalCB):
# do something and call external callback
data = 10 + 12
externalCB(data)
I wanted to make a call to callbackFunction() which is available in main process context from the processes which got created.
But in the above case it creates three new callbackFunction() objects in its own process and calls that which is in its own context.
How to make a call to main callbackFunction() context this using multiprocessing process ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码片段,其中包含使用队列传递适当的回调并将数据传回主进程上的线程的示例:
snippet with example of using a queue to pass appropriate callback, and data back to a thread on the main process: