Python 的并行处理
Parallel Processing on Python
我是 python 和多处理的初学者,所以如果问题看起来很幼稚,请原谅我。
我有两个想同时 运行 的函数。一个是人脸识别的 openCV 实现,另一个是标准 python 代码。
def main():
does(s) # a function call
def face():
recog.main() #another call
如您所料,这两个函数都是最终用户函数,必须调用它们才能执行任务。我希望他们同时 运行。
关于该主题的先前答案建议线程模块,但我已经尝试过但它不起作用。第一个功能。 to be called 首先执行,然后执行第二个。我的一个朋友推荐了 rospy 模块。这是唯一的方法吗?感谢期待。
编辑:在对此 Make 2 functions run at the same time 的回答中,用户写道线程实际上不会同时执行两个函数 运行
我使用 multiprocessing module for 运行 两个函数并行。对于我所做的(更改为您的情况):
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()
我是 python 和多处理的初学者,所以如果问题看起来很幼稚,请原谅我。 我有两个想同时 运行 的函数。一个是人脸识别的 openCV 实现,另一个是标准 python 代码。
def main():
does(s) # a function call
def face():
recog.main() #another call
如您所料,这两个函数都是最终用户函数,必须调用它们才能执行任务。我希望他们同时 运行。
关于该主题的先前答案建议线程模块,但我已经尝试过但它不起作用。第一个功能。 to be called 首先执行,然后执行第二个。我的一个朋友推荐了 rospy 模块。这是唯一的方法吗?感谢期待。
编辑:在对此 Make 2 functions run at the same time 的回答中,用户写道线程实际上不会同时执行两个函数 运行
我使用 multiprocessing module for 运行 两个函数并行。对于我所做的(更改为您的情况):
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()