主进程的多处理回调 - Python

发布于 2025-01-10 20:21:44 字数 708 浏览 0 评论 0原文

有没有办法使用多处理从创建的进程回调主进程? 假设我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2025-01-17 20:21:44

代码片段,其中包含使用队列传递适当的回调并将数据传回主进程上的线程的示例:

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()

snippet with example of using a queue to pass appropriate callback, and data back to a thread on the main process:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文