共计 2233 个字符,预计需要花费 6 分钟才能阅读完成。
如果不打猴子补丁,就会阻塞:
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()
一共花了 9 秒
─➤ 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
如果打猴子补丁,就会不会阻塞:
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()
这个时候,一共就用了 3 秒
─➤ 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
正文完