Python-使用超时连接多个线程
我有多个进程线程正在运行,我想用一个超时参数将它们连接在一起。我知道如果不需要超时,我可以写:Python-使用超时连接多个线程,python,multiprocessing,Python,Multiprocessing,我有多个进程线程正在运行,我想用一个超时参数将它们连接在一起。我知道如果不需要超时,我可以写: for thread in threads: thread.join() 我想到的一个解决方案是使用一个主线程将所有线程连接在一起,并尝试连接该线程。但是,我在Python中收到以下错误: AssertionError: can only join a child process 下面是我的代码 def join_all(threads): for thread in thread
for thread in threads:
thread.join()
我想到的一个解决方案是使用一个主线程将所有线程连接在一起,并尝试连接该线程。但是,我在Python中收到以下错误:
AssertionError: can only join a child process
下面是我的代码
def join_all(threads):
for thread in threads:
thread.join()
if __name__ == '__main__':
for thread in threads:
thread.start()
master = multiprocessing.Process(target=join_all, args=(threads,))
master.start()
master.join(timeout=60)
您可以重复循环每个线程,执行非阻塞检查以查看线程是否完成:
import time
def timed_join_all(threads, timeout):
start = cur_time = time.time()
while cur_time <= (start + timeout):
for thread in threads:
if not thread.is_alive():
thread.join()
time.sleep(1)
cur_time = time.time()
if __name__ == '__main__':
for thread in threads:
thread.start()
timed_join_all(threads, 60)
导入时间
def timed_join_all(线程,超时):
start=cur\u time=time.time()
当cur_time时,下面的代码加入每个进程,等待一定的时间。如果proc返回的速度足够快,则超时时间会减少,然后加入下一个进程。如果发生超时,将显示一条错误消息,并且整个系统将向调用者退出
来源
导入多处理,系统,时间
#启动三个运行时间不同的进程
过程=[
多处理(
target=time.sleep,args=[num],name='%d秒'%num,
)
对于[1,2,5]中的num
]
对于进程中的p:
p、 开始()
打印p
timeleft=3.0
打印“加入,在{}秒后超时”。格式(timeleft)
对于进程中的p:
orig=时间。时间()
打印“{}:join,{:.3f}秒左…”。格式(p,timeleft)
p、 加入(时间限制)
timeleft-=time.time()-orig
如果timeleft我在这里写这篇文章,只是为了确保我不会忘记它。答案的原则与dano的相同。此外,代码片段更像Python:
threads = []
timeout = ...
# create and start the threads
for work in ...:
thread = threading.Thread(target=worker)
thread.daemon = True # without this the thread might outlive its parent
thread.start()
threads.append(thread)
# Wait for workers to finish or for timeout
stop_time = time.time() + timeout
while any(t.isAlive for t in threads) and (time.time() < stop_time):
time.sleep(0.1)
threads=[]
超时=。。。
#创建并启动线程
在……工作:
线程=线程。线程(目标=工作线程)
thread.daemon=True#如果没有此选项,线程可能会比其父线程寿命长
thread.start()
附加(线程)
#等待工作人员完成或等待超时
停止时间=time.time()+超时
而any(t.isAlive表示线程中的t)和(time.time()
这个答案最初是基于dano的答案,但有一些变化
join_all
获取线程列表和超时(以秒为单位),并尝试加入所有线程。它通过对Thread.join
进行非阻塞调用来实现这一点(通过将超时设置为0
,就像不带参数的join
一样)
一旦所有线程都完成(通过检查每个线程上的是否处于活动状态()
),循环将提前退出
如果某些线程在超时发生时仍在运行,则函数将引发一个RuntimeError
,其中包含有关其余线程的信息
import time
def join_all(threads, timeout):
"""
Args:
threads: a list of thread objects to join
timeout: the maximum time to wait for the threads to finish
Raises:
RuntimeError: is not all the threads have finished by the timeout
"""
start = cur_time = time.time()
while cur_time <= (start + timeout):
for thread in threads:
if thread.is_alive():
thread.join(timeout=0)
if all(not t.is_alive() for t in threads):
break
time.sleep(0.1)
cur_time = time.time()
else:
still_running = [t for t in threads if t.is_alive()]
num = len(still_running)
names = [t.name for t in still_running]
raise RuntimeError('Timeout on {0} threads: {1}'.format(num, names))
if __name__ == '__main__':
for thread in threads:
thread.start()
join_all(threads, 60)
导入时间
def join_all(线程,超时):
"""
Args:
线程:要连接的线程对象的列表
超时:等待线程完成的最长时间
提出:
RuntimeError:不是所有线程都在超时前完成了吗
"""
start=cur\u time=time.time()
趁现在
threads = []
timeout = ...
# create and start the threads
for work in ...:
thread = threading.Thread(target=worker)
thread.daemon = True # without this the thread might outlive its parent
thread.start()
threads.append(thread)
# Wait for workers to finish or for timeout
stop_time = time.time() + timeout
while any(t.isAlive for t in threads) and (time.time() < stop_time):
time.sleep(0.1)
import time
def join_all(threads, timeout):
"""
Args:
threads: a list of thread objects to join
timeout: the maximum time to wait for the threads to finish
Raises:
RuntimeError: is not all the threads have finished by the timeout
"""
start = cur_time = time.time()
while cur_time <= (start + timeout):
for thread in threads:
if thread.is_alive():
thread.join(timeout=0)
if all(not t.is_alive() for t in threads):
break
time.sleep(0.1)
cur_time = time.time()
else:
still_running = [t for t in threads if t.is_alive()]
num = len(still_running)
names = [t.name for t in still_running]
raise RuntimeError('Timeout on {0} threads: {1}'.format(num, names))
if __name__ == '__main__':
for thread in threads:
thread.start()
join_all(threads, 60)