关于python:在-eventlet-中使用-gevent-的-sleep-可以避免阻塞吗

35次阅读

共计 766 个字符,预计需要花费 2 分钟才能阅读完成。

不能哦!

from loguru import logger
import gevent
# from gevent import monkey
# monkey.patch_all()

import eventlet
eventlet.monkey_patch()


def foo(i):
    gevent.sleep(1)


pool = eventlet.GreenPool()
for body in pool.imap(foo, [i for i in range(2)]):
    print("got body", body)

运行后果:

╰─➤  time python eventlet_with_gevent_sleep.py 
got body None
got body None
python eventlet_with_gevent_sleep.py  0.39s user 0.05s system 17% cpu 2.515 total

能够看到,一共耗费了 2 秒多


from loguru import logger
import gevent
# from gevent import monkey
# monkey.patch_all()

import eventlet
eventlet.monkey_patch()


def foo(i):
    eventlet.sleep(1)


pool = eventlet.GreenPool()
for body in pool.imap(foo, [i for i in range(2)]):
    print("got body", body)

运行后果:

─➤  time python eventlet_with_gevent_sleep.py
got body None
got body None
python eventlet_with_gevent_sleep.py  0.41s user 0.05s system 29% cpu 1.594 total

应用 eventlet 就变成失常了

正文完
 0