有什么方法可以降低多处理的成本.Process.start()?

时间:2012-09-27 19:40:18

标签: python multiprocessing pickle

所以我一直在使用Python中的线程和进程,并且我一路创建了一个模式,允许同一个类在线程和/或进程之间来回调整,而不会丢失状态数据-name RPC调用和管道。

一切正常,但与从pickle文件加载状态相比,启动进程需要花费大量时间,并且Thread.start()会立即返回,因此只有构造函数的次要成本。那么:在没有荒谬的启动时间的情况下,启动具有大型初始状态的流程的最佳方法是什么。在下面剪切和调试输出,“计数器”的大小刚好超过34,000K,用模式2进行腌制。

...
elif command == "load":
    # RPC call - Loads state from file "pickle_name":
    timestart = time.time()
    print do_remote("take_pickled_state", pickle_name)
    print "Load cost: " + str(time.time() - timestart)
elif command == "asproc":
    if type(_async) is multiprocessing.Process:
        print "Already running as a Process you fool!."
    else:
        do_remote("stop")
        _async.join()
        p_pipe.close()
        p_pipe, c_pipe = multiprocessing.Pipe()
        timestart = time.time()
        _async = multiprocessing.Process(target = counter, args = (c_pipe,))
        # Why is this so expensive!!?!?!?! AAARRG!!?!
        _async.start()
        print "Start cost: " + str(time.time() - timestart)
elif command == "asthread":
    if type(_async) is threading.Thread:
        print "Already running as a Thread you fool!."
    else:

        # Returns the state of counter on stop:
        timestart = time.time()
        counter = do_remote("stop")
        print "Proc stop time: " + str(time.time() - timestart)
        _async.join()
        p_pipe.close()
        p_pipe, c_pipe = multiprocessing.Pipe()
        timestart = time.time()
        _async = threading.Thread(target = counter, args = (c_pipe,))
        _async.start()
        print "Start cost: " + str(time.time() - timestart)
...

相应的调试声明:

Waiting for command...
  >>> load
Load complete.
Load cost: 2.18700003624
Waiting for command...
  >>> asproc
Start cost: 23.3910000324
Waiting for command...
  >>> asthread
Proc stop time: 0.921999931335
Start cost: 0.0629999637604

编辑1: 操作系统:Win XP 64。 Python版本:2.7.x 处理器:Xeon四核。

编辑2: 我真正没有得到的是过程停止返回整个状态需要大约1秒,但是接收状态并开始需要20倍的时间。 (添加了调试输出)

0 个答案:

没有答案