我有一个while loop
让我的相机(带有 opencv)在物体移动时拍照。我也想调用一个函数来播放声音。但是当我调用并播放它时,它将在该执行时间内停止循环。我试过ThreadPoolExecutor
但不知道如何将它与我的代码混合,因为我没有将任何内容传递给函数。只需从循环中调用它即可。顺便提一句。我希望能够多次播放(在执行时多次执行)如果多个something
在代码中出现从循环
相机脚本
from play_it import alert
while True:
#do something in cv2
if "something":
alert() # Here it slowing the loop
and my play_it
script
from playsound import playsound
import concurrent.futures
def alert():
playsound('ss.mp3')
def PlayIt():
with concurrent.futures.ThreadPoolExecutor() as exe:
exe.map(alert, ???) # not sure what to insert here
不知道有什么要求playsound
对于它运行的线程来说,但最简单、最容易做的事情可能就是生成一个线程来播放声音:
import threading
def alert():
threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()
daemon=True
这里将线程作为守护线程启动,这意味着它不会阻止程序退出。 (在 Python 2 上,你必须这样做t = threading.Thread(...); t.daemon = True; t.start()
反而。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)