导入import gevent时报错,提示找不到greenlet模块,在没有找到合适的解决方法时,我卸载重装了gevent库。
C:UsersMACHENIKE>pip3.7 uninstall gevent Uninstalling gevent-1.2.2: Would remove: g:helloworldpythonpython3.7.7libsite-packagesgevent g:helloworldpythonpython3.7.7libsite-packagesgevent-1.2.2-py3.6.egg-info Proceed (y/n)? y Successfully uninstalled gevent-1.2.2
卸载了gevent库之后,重装gevent库时失败,报错:
ERROR: Cannot uninstall 'greenlet'. It is a distutils installed project and thus we cannot accuratel
pip3.7 install gevent Collecting gevent Downloading https://files.pythonhosted.org/packages/e8/78/3852afe86b6406e5a6bdc3bc0cf35fe282eae496ce59b9cf8706f896fc22/gevent-20.9.0-cp37-cp37m-win_amd64.whl (1.5MB) |████████████████████████████████| 1.5MB 63kB/s Requirement already satisfied: cffi>=1.12.2; platform_python_implementation == "CPython" and sys_platform == "win32" in g:helloworldpythonpython3.7.7libsite-packages (from gevent) (1.14.4) Requirement already satisfied: zope.event in g:helloworldpythonpython3.7.7libsite-packages (from gevent) (4.5.0) Collecting zope.interface (from gevent) Downloading https://files.pythonhosted.org/packages/e6/a1/15072b271df268d142cd4377f750836e3e76dfcf9de16107132c84b2f370/zope.interface-5.2.0-cp37-cp37m-win_amd64.whl (196kB) |████████████████████████████████| 204kB 91kB/s Requirement already satisfied: setuptools in g:helloworldpythonpython3.7.7libsite-packages (from gevent) (41.2.0) Collecting greenlet>=0.4.17; platform_python_implementation == "CPython" (from gevent) Downloading https://files.pythonhosted.org/packages/e4/ca/b15607286f4c2592200eb45b4779f22d4673d7575d2b285da00b86fac99c/greenlet-0.4.17-cp37-cp37m-win_amd64.whl Requirement already satisfied: pycparser in g:helloworldpythonpython3.7.7libsite-packages (from cffi>=1.12.2; platform_python_implementation == "CPython" and sys_platform == "win32"->gevent) (2.18) Installing collected packages: zope.interface, greenlet, gevent Found existing installation: greenlet 0.4.12 ERROR: Cannot uninstall 'greenlet'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
解决方法是输入pip install --ignore-installed greenlet命令,忽略软件包是否已经安装,覆盖已安装的文件。
pip install --ignore-installed greenlet Collecting greenlet Using cached https://files.pythonhosted.org/packages/e4/ca/b15607286f4c2592200eb45b4779f22d4673d7575d2b285da00b86fac99c/greenlet-0.4.17-cp37-cp37m-win_amd64.whl Installing collected packages: greenlet Successfully installed greenlet-0.4.17
再次安装gevent库,成功。
pip3.7 install gevent Collecting gevent Using cached https://files.pythonhosted.org/packages/e8/78/3852afe86b6406e5a6bdc3bc0cf35fe282eae496ce59b9cf8706f896fc22/gevent-20.9.0-cp37-cp37m-win_amd64.whl Requirement already satisfied: greenlet>=0.4.17; platform_python_implementation == "CPython" in g:helloworldpythonpython3.7.7libsite-packages (from gevent) (0.4.17) Requirement already satisfied: setuptools in g:helloworldpythonpython3.7.7libsite-packages (from gevent) (41.2.0) Requirement already satisfied: zope.interface in g:helloworldpythonpython3.7.7libsite-packages (from gevent) (5.2.0) Requirement already satisfied: zope.event in g:helloworldpythonpython3.7.7libsite-packages (from gevent) (4.5.0) Requirement already satisfied: cffi>=1.12.2; platform_python_implementation == "CPython" and sys_platform == "win32" in g:helloworldpythonpython3.7.7libsite-packages (from gevent) (1.14.4) Requirement already satisfied: pycparser in g:helloworldpythonpython3.7.7libsite-packages (from cffi>=1.12.2; platform_python_implementation == "CPython" and sys_platform == "win32"->gevent) (2.18) Installing collected packages: gevent Successfully installed gevent-20.9.0
测试代码
import time import gevent def calc_time(func): def wrapper(*args, **kwargs): start_time = time.time() result = func(*args, **kwargs) end_time = time.time() print(f"执行时间是{end_time - start_time}") return result return wrapper def work1(): for i in range(5): print(f"听音乐...{i}") gevent.sleep(1) def work2(): for i in range(5): gevent.sleep(1) print(f"打游戏...{i}") @calc_time def main(): g1 = gevent.spawn(work1) g2 = gevent.spawn(work2) g1.join() g2.join() if __name__ == '__main__': main()
听音乐...0 听音乐...1 打游戏...0 听音乐...2 打游戏...1 听音乐...3 打游戏...2 听音乐...4 打游戏...3 打游戏...4 执行时间是5.031771421432495