因此,我一直在使用 Python 中的线程和进程进行工具化,并且在此过程中,我制作了一种模式,该模式允许同一个类在线程和/或进程之间来回调整,而不会通过使用别名 RPC 丢失状态数据电话和管道。
一切正常,但是与从腌制文件加载状态相比,启动进程需要花费大量时间,并且 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 处理器:至强四核。
编辑 2:我真正不明白的是,进程停止需要大约 1 秒才能返回整个状态,但接收状态和启动需要 20 倍的时间。(添加了调试输出)