Code here:
代码:
from multiprocessing import pool
def worker(num):
print 'Worker:', num
return
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
jobs.append(p)
p.start()
Sorry I am new to python. I am getting the below error whenever I try to import pool. It says something wrong with os.chdir(wdir) but I cant figure out what. Any help ?
抱歉,我是python的新手。每当我尝试导入池时,都会得到下面的错误。它说了一些关于os.chdir(wdir)的问题,但我想不出是什么。任何帮助吗?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\z080302\Desktop\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "C:/Users/z080302/Desktop/Python_Projects/mp_test.py", line 18, in <module>
p = multiprocessing.Process(target=worker, args=(i,))
NameError: name 'multiprocessing' is not defined
2 个解决方案
#1
2
Here's your code:
这是你的代码:
from multiprocessing import pool
def worker(num):
print 'Worker:', num
return
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
jobs.append(p)
p.start()
You have to import the module multiprocessing to use multiprocessing.Process, you have only imported the function/class Pool from multiprocessing, so a simple fix would be:
必须导入模块多处理才能使用多处理。流程,您只从multiprocessing导入了函数/类池,因此一个简单的修复方法是:
import multiprocessing
pool = multiprocessing.pool
def worker(num):
print 'Worker:', num
return
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
jobs.append(p)
p.start()
#2
2
You are importing only pool
module from multiprocessing
module. So your interpreter is aware of pool
only and not of multiprocessing
您只从多处理模块导入池模块。所以您的解释器只知道池,不知道多处理
To resolve the problem, you must import multiprocessing
and when you require the pool
in code you can use it like multiprocessing.pool
要解决这个问题,您必须导入多处理,当您需要代码中的池时,您可以像使用multiprocess. pool一样使用它
multiprocessing
|-- __init__.py
|--Process
|--Pool
|--This_also
Like shown above, you are importing only pool
and python doesn't know who the hell this multiprocessing and Process and This_also are. Usually we have __ init __.py file in python package. A list in this file all = ['pool.py','Process.py',.......'This_also']
contains all the modules contained in the package. So import * will import all the modules. Please go through the https://docs.python.org/2/tutorial/modules.html#
如上面所示,您只导入池,而python不知道这个多处理和进程以及This_also是谁。通常我们有__。python包中的py文件。在这个文件中有一个列表= ['pool.py','Process.py',…'This_also']包含包中包含的所有模块。因此导入*将导入所有模块。请查看https://docs.python.org/2/tutorial/modules.html#
#1
2
Here's your code:
这是你的代码:
from multiprocessing import pool
def worker(num):
print 'Worker:', num
return
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
jobs.append(p)
p.start()
You have to import the module multiprocessing to use multiprocessing.Process, you have only imported the function/class Pool from multiprocessing, so a simple fix would be:
必须导入模块多处理才能使用多处理。流程,您只从multiprocessing导入了函数/类池,因此一个简单的修复方法是:
import multiprocessing
pool = multiprocessing.pool
def worker(num):
print 'Worker:', num
return
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
jobs.append(p)
p.start()
#2
2
You are importing only pool
module from multiprocessing
module. So your interpreter is aware of pool
only and not of multiprocessing
您只从多处理模块导入池模块。所以您的解释器只知道池,不知道多处理
To resolve the problem, you must import multiprocessing
and when you require the pool
in code you can use it like multiprocessing.pool
要解决这个问题,您必须导入多处理,当您需要代码中的池时,您可以像使用multiprocess. pool一样使用它
multiprocessing
|-- __init__.py
|--Process
|--Pool
|--This_also
Like shown above, you are importing only pool
and python doesn't know who the hell this multiprocessing and Process and This_also are. Usually we have __ init __.py file in python package. A list in this file all = ['pool.py','Process.py',.......'This_also']
contains all the modules contained in the package. So import * will import all the modules. Please go through the https://docs.python.org/2/tutorial/modules.html#
如上面所示,您只导入池,而python不知道这个多处理和进程以及This_also是谁。通常我们有__。python包中的py文件。在这个文件中有一个列表= ['pool.py','Process.py',…'This_also']包含包中包含的所有模块。因此导入*将导入所有模块。请查看https://docs.python.org/2/tutorial/modules.html#