導入import gevent時報錯,提示找不到greenlet模塊,在沒有找到合適的解決方法時,我卸載重裝了gevent庫。
C:\Users\MACHENIKE>pip3.7 uninstall gevent Uninstalling gevent-1.2.2: Would remove: g:\helloworld\python\python3.7.7\lib\site-packages\gevent g:\helloworld\python\python3.7.7\lib\site-packages\gevent-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:\helloworld\python\python3.7.7\lib\site-packages (from gevent) (1.14.4) Requirement already satisfied: zope.event in g:\helloworld\python\python3.7.7\lib\site-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:\helloworld\python\python3.7.7\lib\site-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:\helloworld\python\python3.7.7\lib\site-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:\helloworld\python\python3.7.7\lib\site-packages (from gevent) (0.4.17) Requirement already satisfied: setuptools in g:\helloworld\python\python3.7.7\lib\site-packages (from gevent) (41.2.0) Requirement already satisfied: zope.interface in g:\helloworld\python\python3.7.7\lib\site-packages (from gevent) (5.2.0) Requirement already satisfied: zope.event in g:\helloworld\python\python3.7.7\lib\site-packages (from gevent) (4.5.0) Requirement already satisfied: cffi>=1.12.2; platform_python_implementation == "CPython" and sys_platform == "win32" in g:\helloworld\python\python3.7.7\lib\site-packages (from gevent) (1.14.4) Requirement already satisfied: pycparser in g:\helloworld\python\python3.7.7\lib\site-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