Python多处理 – 是否有可能在各个进程之间引入固定的时间延迟?
发布时间:2020-12-20 13:49:56 所属栏目:Python 来源:网络整理
导读:我搜索过,在其他地方找不到这个问题的答案.希望我没有错过任何东西. 我试图使用Python多处理基本上批量运行一些专有模型并行.比方说,我有200次模拟,我想一次批量运行它们~10-20.我的问题是如果两个模型碰巧在相同/相似的时间开始,专有软件会崩溃.我需要在多
我搜索过,在其他地方找不到这个问题的答案.希望我没有错过任何东西.
我试图使用Python多处理基本上批量运行一些专有模型并行.比方说,我有200次模拟,我想一次批量运行它们~10-20.我的问题是如果两个模型碰巧在相同/相似的时间开始,专有软件会崩溃.我需要在多处理产生的进程之间引入延迟,以便每个新模型在启动之前等待一点. 到目前为止,我的解决方案是在子进程开始之前引入一个随机时间延迟,然后再触发模型运行.但是,这只会降低任何两次运行同时启动的可能性,因此在尝试处理大量模型时仍然会遇到问题.因此,我认为时间延迟需要构建到代码的多处理部分,但我无法找到任何文档或示例. 编辑:我使用的是Python 2.7 到目前为止这是我的代码: from time import sleep import numpy as np import subprocess import multiprocessing def runmodels(arg): sleep(np.random.rand(1,1)*120) # this is my interim solution to reduce the probability that any two runs start at the same time,but it isn't a guaranteed solution subprocess.call(arg) # this line actually fires off the model run if __name__ == '__main__': arguments = [big list of runs in here ] count = 12 pool = multiprocessing.Pool(processes = count) r = pool.imap_unordered(runmodels,arguments) pool.close() pool.join() 解决方法
使用线程和信号量的一种方法:
from time import sleep import subprocess import threading def runmodels(arg): subprocess.call(arg) sGlobal.release() # release for next launch if __name__ == '__main__': threads = [] global sGlobal sGlobal = threading.Semaphore(12) #Semaphore for max 12 Thread arguments = [big list of runs in here ] for arg in arguments : sGlobal.acquire() # Block if more than 12 thread t = threading.Thread(target=runmodels,args=(arg,)) threads.append(t) t.start() sleep(1) for t in threads : t.join() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |