使用gevent模块创建协程代码时报错:
pydev debugger: process 10700 is connecting
Connected to pydev debugger (build 183.5912.18)
Traceback (most recent call last):
File "F:\Program Files\JetBrains\PyCharm 2018.3.5\helpers\pydev\pydevd.py", line 1741, in <module>
main()
File "F:\Program Files\JetBrains\PyCharm 2018.3.5\helpers\pydev\pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "F:\Program Files\JetBrains\PyCharm 2018.3.5\helpers\pydev\pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "F:\Program Files\JetBrains\PyCharm 2018.3.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Administrator/PycharmProjects/django_machine_learning/DeepLearning_In_Django/algorithms/gevent.py", line 1, in <module>
import gevent
File "C:\Users\Administrator\PycharmProjects\django_machine_learning\DeepLearning_In_Django\algorithms\gevent.py", line 17, in <module>
th1 = gevent.spawn(work1)
AttributeError: module 'gevent' has no attribute 'spawn'
Process finished with exit code 1
或者:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "F:\Program Files\JetBrains\PyCharm 2018.3.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "F:\Program Files\JetBrains\PyCharm 2018.3.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Administrator/PycharmProjects/django_machine_learning/DeepLearning_In_Django/algorithms/gevent_test2.py", line 11, in <module>
gevent.joinall([
AttributeError: module 'gevent' has no attribute 'joinall'
问题原因:
当前使用的是python3.6版本,gevent为1.2
解决方式:
1.pip uninstall gevent
2.pip uninstall greenlet
2.pip install gevent
安装gevent包时会自动安装greenlet,再次执行代码可以运行
3.将相同的代码复制到另外一个app文件夹下的py文件中,执行正常(可能是本人项目的文件路径环境有问题)