gpt4 book ai didi

Concurrently running two never ending loops in python(在Python中并发运行两个永不结束的循环)

转载 作者:bug小助手 更新时间:2023-10-24 18:40:48 24 4
gpt4 key购买 nike



I'm very new to python and hope the python experts would be able to help me.
I have a program with a while loop in which I'm using speech recognition, in the same time I need to run a another loop in parallel to send keepalive signals to another device.

我是个新手,希望巨蟒专家能帮助我。我有一个带有While循环的程序,我在其中使用语音识别,同时我需要并行运行另一个循环,以向另一个设备发送保活信号。


import speech_recognition as speech_recognition

speech_recognizer = speech_recognition.Recognizer()
speech_microphone = speech_recognition.Microphone()

def listen_to_command():
with speech_microphone as source:

print("Say something...")
try:
run_voice_precessing(source)
except speech_recognition.WaitTimeoutError:
run_voice_precessing(source)

def run_voice_precessing(source):
try:
audio = speech_recognizer.listen(source, timeout=5, phrase_time_limit=10)

text = speech_recognizer.recognize_google(audio)

return text
except handle_error//

//the loop i want to run in parallel

def check_command_timeout():
global last_command_time
while True:
if time.time() - last_command_time > command_timeout :
print("Command timeout reached. Keeping the drone alive.")
// send the command to the device
last_command_time = time.time()

def run_device_control():


if __name__ == '__main__':
while True:
text = listen_to_command()
run_device_control(text)


I've tried using threads, but it doesn't seem to do the job, once the loop starts, it totally ignores the main thread and takes over the process.
I've tried running it in 2 threads, but there is a rosource issue
I've tried running concurrent processes usuing asyncio, but still if one process take a long time to finish the timeout still hangs.

我试过使用线程,但它似乎不起作用,一旦循环开始,它就会完全忽略主线程并接管进程。我试过在两个线程中运行它,但有一个ro源代码问题,我试着使用异步来运行并发进程,但如果一个进程需要很长时间才能完成,超时仍然挂起。


async def run_device_control():
while True:
text = listen_to_command()
run_device_control(text)
# if this takes a long time to execute check_timeout waits unitl this finishes
await asyncio.sleep(1)


async def check_timeout():
global last_command_time
global time_since_last_time_out
global init_position
last_time_out = time.time()
while True:
print("---------------------------------------------------------- ")
print("Keep alive : ", time.time() - last_time_out)
print("---------------------------------------------------------- ")
last_time_out = time.time()
await asyncio.sleep(5)


async def main():
await asyncio.gather(run_drone_control(), check_timeout())


I tried multiprocessing too

我也尝试过多进程处理


    process2 = multiprocessing.Process(target=check_timeout())

process1.start()
process2.start()

process1.join()
process2.join()

this doesn't even start the process1

这甚至不会启动进程1


What else can I try? Any help would be very much appreciated.

我还能试什么呢?任何帮助都将不胜感激。


更多回答

Right now your while True is not blocking and eating 100% of the CPU (although python will give other threads time slices). Is there a reason why you can't just time.sleep(command_timeout)? Is something else also setting last_command_time?

现在,您的While True并没有阻塞和占用100%的CPU(尽管Python会给其他线程提供时间片)。为什么你不能只是时间睡眠(COMMAND_TIMEORT)?是否还有其他东西在设置LAST_COMMAND_TIME?

yes, depending on the output of the speech_recognition I am executing some functions (mostly vision algorithms, which take a bit of time). And the users might not give commands at regular intervals. @tdelaney

是的,根据语音识别的输出,我正在执行一些函数(主要是视觉算法,这需要一些时间)。并且用户可能不会以规则的间隔发出命令。@tdelaney

don't call the function when you pass it as target to Process (get rid of the extra parenthesis; ... target=check_timeout)) in doing so, you're calling the function in the main process and the return value will be assigned to target rather than the function itself.

当你将函数作为目标传递给Process时,不要调用它(去掉多余的括号;... target=check_timeout))这样做时,您在主进程中调用函数,返回值将分配给target而不是函数本身。

优秀答案推荐
更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com