提问人: 提问时间:3/1/2022 更新时间:3/3/2022 访问量:701
主进程的多处理回调 - Python
Multiprocessing Callbacks to Main Process - Python
问:
有没有办法使用MultiProcessing从创建的进程回调到主进程? 假设我有 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()上下文?
答:
1赞
Aaron
3/2/2022
#1
片段,其中包含使用队列将适当的回调和数据传递回主进程上的线程的示例:
from multiprocessing import Process, Queue, current_process
from threading import Thread
from time import sleep
def foo_process(data, cb_queue, cb):
print(f"data from process [{current_process().pid}]: {data}")
sleep(.1) # time to flush stdout so print statement is usually in correct order
# (may still be out of order on IPython)
cb_queue.put((cb, data))
def foo_thread(data):
print(f"data from cb_thread in process [{current_process().pid}]: {data*2}")
def callback_caller(cb_queue):
for func, *args in iter(cb_queue.get, None): #pass None to exit thread
func(*args)
if __name__ == "__main__":
print(f"main pid: [{current_process().pid}]")
q = Queue()
p = Process(target=foo_process, args=(3.15, q, foo_thread))
p.start()
#It's advisable (if possible) to always create processes first then threads.
#this primarily applies to using "fork", but it's good to know / good habit.
t = Thread(target=callback_caller, args=(q,))
t.start()
p.join()
q.put(None) #send shutdown signal to callback_caller
t.join()
评论
Queue
my_queue.put(data)