Python上的并行处理

10 浏览
0 Comments

Python上的并行处理

这个问题已经在这里有了答案:

如何在Python中使用线程?

使用Multiprocessing队列、池和锁的简单示例

我是Python和多进程的初学者,如果问题看起来有些幼稚,请原谅我。

def main():
    does(s) # a function call 
def face():
    recog.main() #another call

我有两个函数要同时运行。一个是面部识别的openCV实现,另一个是标准Python代码。

正如你所猜想的那样,这两个函数是最终的终端用户函数,需要调用它们来实现任务。我希望它们两个同时运行。

以前这个问题的答案建议使用线程模块,但我已经尝试过了,它不起作用。我的一个朋友推荐了rospy模块。这是唯一的方法吗?感谢您的期待。

编辑:在这个答案中,让2个函数同时运行,一个用户写了线程实际上不能让两个函数同时运行。

admin 更改状态以发布 2023年5月23日
0
0 Comments

我使用multiprocessing模块来并行运行两个函数。我的方式如下(根据您的情况进行修改):

import multiprocessing
def main():
    does(s) # a function call 
def face():
    recog.main() #another call
# Initiate two workers for the two functions
workerMAIN = multiprocessing.Process(target=main)
workerFACE = multiprocessing.Process(target=face)
# Start the workers
workerMAIN.start()
workerFACE.start()
# Wait until the functions have finished
workerMAIN.join()
workerFACE.join()

0