多线程,无法运行Process命令 [英] Multithreading, can't run Process command

查看:110
本文介绍了多线程,无法运行Process命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下代码:

#!/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()

我得到的输出是:

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'

推荐答案

您正尝试从本地目录而不是python库import multiprocessing. python解释器首先尝试从当前目录导入模块.当目录中有一个名为multiprocessing.pyc的文件时,解释器将尝试导入该文件.因此,您遇到了错误.因此,删除multiprocessing.pyc将有助于解决您的问题.

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.

这篇关于多线程,无法运行Process命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆