Without monkey patching, it blocks:

 import eventlet
# eventlet.monkey_patch()
from loguru import logger
import subprocess

def func():
    p = subprocess.Popen('time sleep 3', stderr=subprocess.STDOUT,
                         stdout=subprocess.PIPE, shell=True, close_fds=True)
    out_str, err_str = p.communicate()
    p.wait()
    _process_result = out_str.decode(encoding='utf-8')

    logger.debug(_process_result)


pool = eventlet.GreenPool()

pool.spawn_n(func)
pool.spawn_n(func)
pool.spawn_n(func)

pool.waitall()

It took 9 seconds in total

 ─➤  time python -u "/home/bot/Desktop/ideaboom/test_evenetlet/need_lock_with_sleep.py"
2022-08-30 01:04:52.159 | DEBUG    | __main__:func:22 - 0.00user 0.00system 0:03.00elapsed 0%CPU (0avgtext+0avgdata 2240maxresident)k
0inputs+0outputs (0major+124minor)pagefaults 0swaps

2022-08-30 01:04:55.163 | DEBUG    | __main__:func:22 - 0.00user 0.00system 0:03.00elapsed 0%CPU (0avgtext+0avgdata 2240maxresident)k
0inputs+0outputs (0major+124minor)pagefaults 0swaps

2022-08-30 01:04:58.172 | DEBUG    | __main__:func:22 - 0.00user 0.00system 0:03.00elapsed 0%CPU (0avgtext+0avgdata 2208maxresident)k
0inputs+0outputs (0major+122minor)pagefaults 0swaps

python -u "/home/bot/Desktop/ideaboom/test_evenetlet/need_lock_with_sleep.py"  0.29s user 0.03s system 3% cpu 9.316 total

If you monkey patch, it won't block:

 import eventlet
eventlet.monkey_patch()
from loguru import logger
import subprocess

def func():
    p = subprocess.Popen('time sleep 3', stderr=subprocess.STDOUT,
                         stdout=subprocess.PIPE, shell=True, close_fds=True)
    out_str, err_str = p.communicate()
    p.wait()
    _process_result = out_str.decode(encoding='utf-8')

    logger.debug(_process_result)


pool = eventlet.GreenPool()

pool.spawn_n(func)
pool.spawn_n(func)
pool.spawn_n(func)

pool.waitall()

At this time, it took a total of 3 seconds

 ─➤  time python -u "/home/bot/Desktop/ideaboom/test_evenetlet/need_lock_with_sleep.py"
2022-08-30 00:41:48.751 | DEBUG    | __main__:func:22 - 0.00user 0.00system 0:03.00elapsed 0%CPU (0avgtext+0avgdata 2244maxresident)k
0inputs+0outputs (0major+123minor)pagefaults 0swaps

2022-08-30 00:41:48.752 | DEBUG    | __main__:func:22 - 0.00user 0.00system 0:03.00elapsed 0%CPU (0avgtext+0avgdata 2224maxresident)k
0inputs+0outputs (0major+123minor)pagefaults 0swaps

2022-08-30 00:41:48.752 | DEBUG    | __main__:func:22 - 0.00user 0.00system 0:03.00elapsed 0%CPU (0avgtext+0avgdata 2224maxresident)k
0inputs+0outputs (0major+122minor)pagefaults 0swaps

python -u "/home/bot/Desktop/ideaboom/test_evenetlet/need_lock_with_sleep.py"  0.42s user 0.03s system 13% cpu 3.446 total

universe_king
3.4k 声望678 粉丝