Multithreading, can't run Process command

2019-07-20 12:29发布

I am trying to the following code:

#!/usr/bin/python
import multiprocessing

def f(name): 
print 'hello', name

if __name__ == '__main__':
    p = multiprocessing.Process(target=f, args=('bob',))
    p.start()
    p.join()

The output I get is :

Traceback (most recent call last):
  File "a.py", line 9, in <module>
    p = multiprocessing.Process(target=f, args=('bob',))
AttributeError: 'module' object has no attribute 'Process'

3条回答
地球回转人心会变
2楼-- · 2019-07-20 13:11

You are trying to import multiprocessing from your local directory and not from the python library. The python interpreter first tries to import the module from the present directory. As you have got a file with the name multiprocessing.pyc in your directory, the interpreter is trying to import that. Hence you have got the error. Thus deleting multiprocessing.pyc will help resolve your problem.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-07-20 13:15

The mistake was naming, my script as 'multiprocessing.py', once it was created. I made an another script with name 'a.py' and both of them were not working. After listing the directories, 'multiprocessing.pyc' was located. I deleted this file, and executed 'a.py' file which executed like gem! thanks to @Bhargav Rao for highlighting

查看更多
叛逆
4楼-- · 2019-07-20 13:15

Dont give the name of the file as "multiprocessing.py", give any other

thanks, vybhav

查看更多
登录 后发表回答