Python多处理 - 是否有可能在各个进程之间引入固定的时间延迟?

时间:2015-05-20 07:28:14

标签: python multithreading batch-file multiprocessing

我搜索过,在其他地方找不到这个问题的答案。希望我没有错过任何东西。

我正在尝试使用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()

2 个答案:

答案 0 :(得分:1)

multiprocessing.Pool()已经限制同时运行的进程数。

您可以使用锁定来分隔进程的开始时间(未测试):

import threading
import multiprocessing

def init(lock):
    global starting
    starting = lock

def run_model(arg):
    starting.acquire() # no other process can get it until it is released
    threading.Timer(1, starting.release).start() # release in a second
    # ... start your simulation here

if __name__=="__main__":
   arguments = ...
   pool = Pool(processes=12, 
               initializer=init, initargs=[multiprocessing.Lock()])
   for _ in pool.imap_unordered(run_model, arguments):
       pass

答案 1 :(得分:0)

使用线程和信号量执行此操作的一种方法:

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()