在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() 展开
from multiprocessing import *
def test(x):
print current_process().pid, x
time.sleep(1)
p = Process(target = test, args = [100])
p.start()
p.join() 展开
1个回答
展开全部
看文档:
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.
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.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |