简体   繁体   English

在python中使用多重处理运行多个进程的问题

[英]Problem in running multiple processes using multiprocessing in python

I have 3 functions in my program audio(), motion() and vibration() and each of which is defined in such a way that they collect data from their respective sensors. 我的程序中有3个函数audio(),motion()和振动(),每个函数的定义方式都使它们从各自的传感器收集数据。 I wrote a python program that collects data from audio and vibration sensor and a C program to get data from motion sensor (it was much faster than python). 我编写了一个python程序,该程序从音频和振动传感器收集数据,并编写了一个C程序从运动传感器获取数据(它比python快得多)。

Initially I was using 'Processes' from multiprocessing library to run audio() and vibration() simultaneously which worked absolutely fine. 最初,我使用多处理库中的“ Processes”来同时运行audio()和振动(),这绝对好用。 But now when I am trying run motion() along with them both, then the problem comes to existence. 但是现在当我尝试同时运行motion()和它们时,问题就存在了。 The issue is that the motion() process does not starts until the audio() is completed or vice versa and since it is supposed to be running on realtime, I need to give keyboard interrupt in order to finish audio() or motion() process. 问题是motion()进程直到audio()完成才开始,反之亦然,并且由于它应该实时运行,因此我需要给键盘中断以完成audio()或motion()处理。

From my understanding either process (motion and audio) waits for each other to complete and then starts execution. 根据我的理解,任何一个过程(运动和音频)都互相等待完成,然后开始执行。 But my goal is to run them all together so that I can get data on the same timeline. 但是我的目标是将它们一起运行,以便我可以在同一时间轴上获取数据。

def vibration():
    #collects vibration data from raspberry shake
    ....
    ....

def audio():
    #uses pyaudio to record audio data
    ....
    ....

def motion():
    command = 'rtl_sdr -f 433000000 -g 15 -s 1024000 - | ./rf_receiver'
    subprocess.run(command)

p1 = Process(target=_thread.start_new_thread(fetch_data, ("hello",)))
p1.start()
p2 = Process(target=audio())
p2.start()
p3 = Process(target=motion())
p3.start()

p1.join()
p2.join()
p3.join()

Your problem is that you're calling your worker functions from your main process and only passing their return values as the target parameter to Process . 您的问题是,您正在从主流程中调用辅助函数,并且仅将其返回值作为target参数传递给Process You probably want something like p2 = Process(target=audio) (without the () after audio ), and p3 = Process(target=motion) . 您可能想要类似p2 = Process(target=audio) (在audio之后没有() )和p3 = Process(target=motion) You probably also want something similar for p1 , but I have no idea what your current code is doing with threads there, so I can't fix it easily. 您可能还希望为p1类似的功能,但我不知道您当前的代码正在使用那里的线程做什么,因此我无法轻松修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM