在windows下,是不是不能使用python的multiprocessing块呀,就像不能使用os.fork()一样,例子如下:

importos,time,sys,sysfrommultiprocessingimport*deftest(x):printcurrent_process().pid,... import os, time,sys,sys
from multiprocessing import *
def test(x):
print current_process().pid, x
time.sleep(1)

p = Process(target = test, args = [100])
p.start()
p.join()
展开
 我来答
牛长旭0Fk
2011-08-04 · 超过18用户采纳过TA的回答
知道答主
回答量:32
采纳率:0%
帮助的人:54.9万
展开全部
看文档:
python-2.7-docs-html/library/multiprocessing.html#multiprocessing-programming

简而言之,需要在p=Process()前加上
if __name__ == ‘__main__’:
p = Process(...

Safe importing of main module

Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process).

For example, under Windows running the following module would fail with a RuntimeError:

from multiprocessing import Process

def foo():
print 'hello'

p = Process(target=foo)
p.start()

Instead one should protect the “entry point” of the program by using if __name__ == '__main__': as follows:

from multiprocessing import Process, freeze_support

def foo():
print 'hello'

if __name__ == '__main__':
freeze_support()
p = Process(target=foo)
p.start()

(The freeze_support() line can be omitted if the program will be run normally instead of frozen.)

This allows the newly spawned Python interpreter to safely import the module and then run the module’s foo() function.
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式