【问题标题】:Parallel Processing on Python [duplicate]Python上的并行处理[重复]
【发布时间】:2016-10-16 05:11:24
【问题描述】:

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

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

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

您可以猜到,这两个函数都是最终用户函数,必须调用它们才能实现任务。我希望它们同时运行。

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

编辑:在回答这个问题时,Make 2 functions run at the same time,一位用户写道,线程实际上不会使两个函数同时运行

【问题讨论】:

标签: python multithreading multiprocessing ros


【解决方案1】:

我使用multiprocessing module 来并行运行两个函数。对于我所做的(更改为您的情况):

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

【讨论】:

  • 非常感谢先生!我曾使用 target 关键字尝试过多处理模块,但它没有奏效。不过,这是一个后续问题。名称是必要的参数吗?谢谢
  • 不,不是:在example in 16.6.1.1 中,您可以看到Process(target=f, args=('bob',))。所以你可以删除名称部分!我会更新我的答案:)
  • 对我来说,这也是我正在寻找的例子。就像 @Spock 我查看了建议的重复项,但直到我实现了上面的代码才能让它工作
猜你喜欢
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 2021-01-29
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
相关资源
最近更新 更多