<p>中的一些多处理示例python.org网站我不太清楚,很容易从一个有缺陷的设计开始。下面是一个简单化的例子,让我开始一个项目:</p>
<pre><code>import os, time, random, multiprocessing
def busyfunc(runseconds):
starttime = int(time.clock())
while 1:
for randcount in range(0,100):
testnum = random.randint(1, 10000000)
newnum = testnum / 3.256
newtime = int(time.clock())
if newtime - starttime > runseconds:
return
def main(arg):
print 'arg from init:', arg
print "I am " + multiprocessing.current_process().name
busyfunc(15)
if __name__ == '__main__':
p = multiprocessing.Process(name = "One", target=main, args=('passed_arg1',))
p.start()
p = multiprocessing.Process(name = "Two", target=main, args=('passed_arg2',))
p.start()
p = multiprocessing.Process(name = "Three", target=main, args=('passed_arg3',))
p.start()
time.sleep(5)
</code></pre>
<p>这将使3个处理器运行15秒。它应该很容易修改为更多。也许这将有助于调试您当前的代码,并确保您真正生成多个独立的进程。在</p>
<p>如果由于RAM限制而必须共享数据,那么我建议您:
<a href="http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes" rel="nofollow">http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes</a></p>