Python上的并行处理 [英] Parallel Processing on Python

查看:66
本文介绍了Python上的并行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python和多处理程序的初学者,所以如果问题看起来很幼稚,请原谅我. 我有两个要同时运行的功能.一个是人脸识别的openCV实现,另一个是标准的python代码.

I am a beginner in python and multiprocessing so if the question seems naive please forgive me. I have two functions that I want to run at the same time. One is an openCV implementation of face recognition and the other is a standard python code.

def main():
    does(s) # a function call 

def face():
    recog.main() #another call

您可能会猜到,这两个功能都是实现该任务必须调用的最终最终用户功能.我希望它们都同时运行.

As you can guess, both the functions are the final end user functions that one has to call to implement the task. I want them both to run simutaneously.

关于此主题的先前答案建议线程模块,但我已经尝试过了,但它不起作用.第一个功能.先执行被调用,然后再执行第二个.我的一个朋友推荐rospy模块.这是唯一的方法吗?谢谢您的期待.

Previous answers on this topic advise threading module but I have tried it and it does not work. The first func. to be called is executed first and then the second one. A friend of mine recommended rospy module. Is it the only way? Thanks in anticipation.

在此问题的答案中,

In the answer to this, Make 2 functions run at the same time , a user has written that threading actually won't make two functions run at the same time

推荐答案

我使用 multiprocessing模块以并行运行两个功能.对于我所做的(更改为您的情况):

I use the multiprocessing module for running two functions parallel. For what I did (changed to your situation):

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上的并行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆