gevent协程之猴子补丁带来的坑
我们都知道使用gevent协程时,经常会看见在导入包的时候看见这样的代码
from gevent import monkey; monkey.patch_all()
monkey.patch_all()作用呢,就是将阻塞的改成非阻塞
具体有哪些呢。

Example: 执行发现报错了。loop_exit,线程异常退出。线程改成非阻塞失败。(__此问题摘自遇到此问题的一个微友)
import random, datetime, time, json
import gevent
from gevent.pool import Pool from gevent import monkey; monkey.patch_all()
import requests ywdata = [(0, '', 0, 0, '', 1, 1, 0, 0), (1, '', 0, 0, '', 1, 1, 0, 0), (2, '', 0, 0, '', 1, 1, 0, 0), (3, '', 0, 0, '', 1, 1, 0, 0), (4, '', 0, 0, '', 1, 1, 0, 0), (5, '', 0, 0, '', 1, 1, 0, 0), (6, '', 0, 0, '', 1, 1, 0, 0), (7, '', 0, 0, '', 1, 1, 0, 0), (8, '', 0, 0, '', 1, 1, 0, 0), (9, '', 0, 0, '', 1, 1, 0, 0), (10, '', 0, 0, '', 1, 1, 0, 0), (11, '', 0, 0, '', 1, 1, 0, 0), (12, '', 0, 0, '', 1, 1, 0, 0), (13, '', 0, 0, '', 1, 1, 0, 0), (14, '', 0, 0, '', 1, 1, 0, 0), (15, '', 0, 0, '', 1, 1, 0, 0), (16, '', 0, 0, '', 1, 1, 0, 0), (17, '', 0, 0, '', 1, 1, 0, 0), (0, '', 0, 0, '', 0, 1, 0, 0), (1, '', 0, 0, '', 0, 1, 0, 0), (2, '', 0, 0, '', 0, 1, 0, 0), (3, '', 0, 0, '', 0, 1, 0, 0), (4, '', 0, 0, '', 0, 1, 0, 0), (5, '', 0, 0, '', 0, 1, 0, 0), (6, '', 0, 0, '', 0, 1, 0, 0), (7, '', 0, 0, '', 0, 1, 0, 0), (8, '', 0, 0, '', 0, 1, 0, 0), (9, '', 0, 0, '', 0, 1, 0, 0), (10, '', 0, 0, '', 0, 1, 0, 0), (11, '', 0, 0, '', 0, 1, 0, 0), (12, '', 0, 0, '', 0, 1, 0, 0), (13, '', 0, 0, '', 0, 1, 0, 0), (14, '', 0, 0, '', 0, 1, 0, 0), (15, '', 0, 0, '', 0, 1, 0, 0), (16, '', 0, 0, '', 0, 1, 0, 0), (17, '', 0, 0, '', 0, 1, 0, 0), (0, '', 1, 1, '', 1, 2, 0, 0), (1, '', 1, 1, '', 1, 2, 0, 0), (2, '', 1, 1, '', 1, 2, 0, 0), (3, '', 1, 1, '', 1, 2, 0, 0), (4, '', 1, 1, '', 1, 2, 0, 0), (5, '', 1, 1, '', 1, 2, 0, 0), (6, '', 1, 1, '', 1, 2, 0, 0), (7, '', 1, 1, '', 1, 2, 0, 0), (8, '', 1, 1, '', 1, 2, 0, 0), (9, '', 1, 1, '', 1, 2, 0, 0), (10, '', 1, 1, '', 1, 2, 0, 0), (11, '', 1, 1, '', 1, 2, 0, 0), (12, '', 1, 1, '', 1, 2, 0, 0), (13, '', 1, 1, '', 1, 2, 0, 0), (14, '', 1, 1, '', 1, 2, 0, 0), (15, '', 1, 1, '', 1, 2, 0, 0), (16, '', 1, 1, '', 1, 2, 0, 0), (17, '', 1, 1, '', 1, 2, 0, 0), (0, '', 1, 1, '', 0, 2, 0, 0), (1, '', 1, 1, '', 0, 2, 0, 0), (2, '', 1, 1, '', 0, 2, 0, 0), (3, '', 1, 1, '', 0, 2, 0, 0), (4, '', 1, 1, '', 0, 2, 0, 0), (5, '', 1, 1, '', 0, 2, 0, 0), (6, '', 1, 1, '', 0, 2, 0, 0), (7, '', 1, 1, '', 0, 2, 0, 0), (8, '', 1, 1, '', 0, 2, 0, 0), (9, '', 1, 1, '', 0, 2, 0, 0), (10, '', 1, 1, '', 0, 2, 0, 0), (11, '', 1, 1, '', 0, 2, 0, 0), (12, '', 1, 1, '', 0, 2, 0, 0), (13, '', 1, 1, '', 0, 2, 0, 0), (14, '', 1, 1, '', 0, 2, 0, 0), (15, '', 1, 1, '', 0, 2, 0, 0), (16, '', 1, 1, '', 0, 2, 0, 0), (17, '', 1, 1, '', 0, 2, 0, 0)] user_id = 353208547
base_product_no = "yacx_sebwylx_sy3"
api_url = 'https://api.xxxx.com'
num = 1 def calculate(api_url, birthday, pytype, limit, frequency, hasSocialSecurity, base_product_no, price):
"""
试运算保费
:param api_url:
:param birthday:
:param pytype:
:param limit:
:param frequency:
:param hasSocialSecurity:
:param base_product_no:
:param price:
:return:
"""
url = "{}/api/sdb/order/cal/calculate".format(api_url)
headers = {
'Content-Type': "application/json",
}
payload = {"birthday": birthday, "paymentMethod": "{}_{}_{}".format(pytype, limit, frequency), "hasSocialSecurity":hasSocialSecurity,"baseProductNo":base_product_no}
# print (payload)
payload = json.dumps(payload)
response = requests.request("POST", url, data=payload, headers=headers)
res = response.json() if res["code"] == 0:
if res["data"]['calPremium'] == price:
return ('{"code":0,"status":succes}}')
else:
return res pool = Pool(20) taskall = [] j = 0
for i in range(len(ywdata)):
if ywdata[i][0] != 0:
taskall.append(pool.spawn(calculate, api_url, ywdata[i][1], ywdata[i][6], ywdata[i][2], ywdata[i][3], ywdata[i][5], base_product_no, ywdata[i][4]))
j += 1 gevent.joinall(taskall, timeout=3, raise_error=False) for l in taskall:
print(l.value) print('一共跑{}个数据'.format(j))
Error:
Traceback (most recent call last):
File "D:\test_project\venv\pytest_env\lib\site-packages\gevent\_ffi\loop.py", line 446, in _run_callbacks
callback(*args)
File "src\gevent\_abstract_linkable.py", line 114, in gevent.__abstract_linkable.AbstractLinkable._notify_links
File "src\gevent\_abstract_linkable.py", line 148, in gevent.__abstract_linkable.AbstractLinkable._notify_links
AssertionError
2019-10-23T02:48:00Z <callback at 0x5578850 args=()> failed with AssertionError {"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
一共跑68个数据
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "src\gevent\_abstract_linkable.py", line 169, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src\gevent\_greenlet_primitives.py", line 60, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\gevent\_greenlet_primitives.py", line 60, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\gevent\_greenlet_primitives.py", line 64, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
Exception in thread ptvsd.stopping:
Traceback (most recent call last):
File "D:\python36\Lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "D:\test_project\venv\pytest_env\lib\site-packages\gevent\threading.py", line 177, in run
super(Thread, self).run()
File "D:\python36\Lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd\wrapper.py", line 692, in stop
wait()
File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd\daemon.py", line 453, in wait_debugger
lock_wait(lock, timeout)
File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd\_util.py", line 104, in lock_wait
if not _lock_acquire(lock, timeout):
File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd\_util.py", line 113, in _lock_acquire
return lock.acquire(timeout=timeout)
File "D:\test_project\venv\pytest_env\lib\site-packages\gevent\thread.py", line 84, in acquire
return BoundedSemaphore.acquire(self, blocking, timeout)
File "src\gevent\_semaphore.py", line 100, in gevent.__semaphore.Semaphore.acquire
File "src\gevent\_semaphore.py", line 128, in gevent.__semaphore.Semaphore.acquire
File "src\gevent\_abstract_linkable.py", line 192, in gevent.__abstract_linkable.AbstractLinkable._wait
File "src\gevent\_abstract_linkable.py", line 165, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src\gevent\_abstract_linkable.py", line 169, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src\gevent\_greenlet_primitives.py", line 60, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\gevent\_greenlet_primitives.py", line 60, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\gevent\_greenlet_primitives.py", line 64, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\gevent\__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch
gevent.exceptions.LoopExit: This operation would block forever
Hub: <Hub '' at 0x50f70e0 default pending=0 ref=0 thread_ident=0x6df8>
Handles:
[HandleState(handle=<cdata 'struct uv_handle_s *' 0x06E08EA0>, type=b'check', watcher=<loop at 0x6741790 default pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06C353D8>, type=b'timer', watcher=<loop at 0x6741790 default pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06E08D80>, type=b'prepare', watcher=<loop at 0x6741790 default pending=0 ref=0>, ref=0, active=1,
closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06E08CC0>, type=b'check', watcher=<loop at 0x6741790 default pending=0 ref=0>, ref=1, active=0, closing=0)] File "src\gevent\__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch
gevent.exceptions.LoopExit: This operation would block forever
Hub: <Hub '' at 0x50f73a0 default default pending=0 ref=0 resolver=<gevent.resolver_thread.Resolver at 0x52ae330 pool=<ThreadPool at 0x52ae490 0/10/10 hub=<Hub at 0x50f73a0 thread_ident=0x29244>>> threadpool=<ThreadPool at 0x52ae490 0/10/10 hub=<Hub at 0x50f73a0 thread_ident=0x29244>> thread_ident=0x723c>
Handles:
[HandleState(handle=<cdata 'struct uv_handle_s *' 0x0090E940>, type=b'check', watcher=<loop at 0x5106970 default default pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x0091BB80>, type=b'timer', watcher=<loop at 0x5106970 default default pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x0090E9A0>, type=b'prepare', watcher=<loop at 0x5106970 default default pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x0090EB20>, type=b'check', watcher=<loop at 0x5106970 default default pending=0 ref=0>, ref=1, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x034B50A0>, type=b'async', watcher=<async_ at 0x52ae4d0 callback=<bound method ThreadPool._on_fork
of <ThreadPool at 0x52ae490 0/10/10 hub=<Hub at 0x50f73a0 thread_ident=0x29244>>> args=() watcher=<cdata 'struct uv_async_s *' owning 116 bytes> handle=<cdata 'void *' 0x052A28A0> ref=False>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x0088B498>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D45BF0>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D44F08>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8C510>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8C9B0>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8C888>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8CAD8>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8CC00>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8C198>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8BE20>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8C2C0>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8BF48>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8C638>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8C3E8>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8C760>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04D8C070>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04DFBE38>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04DFBBE8>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04DFC400>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04DFC528>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04DFCAF0>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04DFC8A0>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04DFC2D8>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x04DFCC18>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06D96E40>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06D96AC8>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06D96D18>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06D95A98>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06D955F8>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06D96188>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06D95E10>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x070DD0C8>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x070DC538>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x070DCC28>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x070DC9D8>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x070DC1C0>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x06D96060>, type=b'poll', watcher=None, ref=1, active=0, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x070DDC58>, type=b'poll', watcher=None, ref=1, active=0, closing=0)]
看着报错信息,头疼。但其实呢,解决办法很简单,真的很简单。
只要一步,问题解决。 thread=False
from gevent import monkey; monkey.patch_all(thread=False)
加上thread=False在试下,结果:一切正常,没有在报错。

{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
{"code":0,"status":succes}}
一共跑68个数据
需要注意的点:
1. 导入包的位置。 补丁以下都会被改成阻塞。 注意位止,这个坑了很多人。
比如1: 这么放执行requests请求时没有问题。

2:果断报错,原因,并没有将requests改成阻塞。

2.开启多线程时,monkey会阻止主线程执行,所以这里设置,thread=False
gevent协程之猴子补丁带来的坑的更多相关文章
- 协程gevent模块和猴子补丁
# pip 装模块 greenlet和gevent # 协程 # 与进程.线程一样也是实现并发的手段 # 创建一个线程.关闭一个线程都需要创建寄存器.栈等.需要消耗时间 # 协程本质上是一个线程 # ...
- python协程初步--gevent库使用以及解释什么是猴子补丁monkey_patch
协程工作的特点是遇到阻塞或耗时的任务时就切换,协程的生存依赖于线程,线程依赖于进程 一个似乎有点问题的例子 import gevent,time def kisscpc(num): for i in ...
- python并发编程之gevent协程(四)
协程的含义就不再提,在py2和py3的早期版本中,python协程的主流实现方法是使用gevent模块.由于协程对于操作系统是无感知的,所以其切换需要程序员自己去完成. 系列文章 python并发编程 ...
- day38 各种队列、Event事件、协程、猴子补丁
1.各种队列 我们已经学习了队列这种存取值的方法,我们以前使用的队列是可以进行进程间通信的(IPC),但是今天学习的这两种队列是不能进行进程间通信的,只能进行线程间的通信 这两种队列分别是先进后出式队 ...
- Gevent和猴子补丁
定义 在2018年看Flutent python时了解到猴子补丁,知道咋回事,但是现在通过代码更深刻认识猴子补丁. 猴子补丁:在运行时修改类或模块,而不改动源码. 例子1 没有用猴子补丁 import ...
- Python之路-python(Queue队列、进程、Gevent协程、Select\Poll\Epoll异步IO与事件驱动)
一.进程: 1.语法 2.进程间通讯 3.进程池 二.Gevent协程 三.Select\Poll\Epoll异步IO与事件驱动 一.进程: 1.语法 简单的启动线程语法 def run(name): ...
- python中的猴子补丁Monkey Patch
python中的猴子补丁Monkey Patch 什么是猴子补丁 the term monkey patch only refers to dynamic modifications of a cla ...
- 什么是协程?与线程和进程对比优劣在哪?gevent协程示例代码
协程 协程,又称微线程,纤程.英文名Coroutine..一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在 ...
- (20)gevent协程
协程: 也叫纤程,协程是线程的一种实现,指的是一条线程能够在多任务之间来回切换的一 种实现,对于CPU.操作系统来说,协程并不存在 任务之间的切换会花费时间.目前电脑配置一般线程开到200会阻塞卡顿 ...
随机推荐
- [USACO06FEB]摊位预订Stall Reservations(贪心)
[USACO06FEB]摊位预订Stall Reservations 题目描述 Oh those picky N (1 <= N <= 50,000) cows! They are so ...
- AES apache commons-crypto 对称加密
apache实现的AES256加密 官方用户指导链接:http://commons.apache.org/proper/commons-crypto/userguide.html 官方字节缓存实现的例 ...
- 如何开启spring框架以注解形式的配置
步骤 导包(新版本需要导入spring-aop-4.3.17.RELEASE.jar) 为配置文件applicationContext.xml引入新的命名空间(约束) 开启使用注解 <?xml ...
- 在项目中使用 Maven 私服
#在项目中使用 Maven 私服 在 Maven settings.xml 中添加 Nexus 认证信息(servers 节点下): <server> <id>nexus-re ...
- 【leetcode】900. RLE Iterator
题目如下: 解题思路:非常简单的题目,直接递归就行了. 代码如下: class RLEIterator(object): def __init__(self, A): ""&quo ...
- 【leetcode】543. Diameter of Binary Tree
题目如下: 解题思路:最长的周长一定是树中某一个节点(不一定是根节点)的左右子树中的两个叶子节点之间的距离,所以最简单的办法就是把树中所有节点的左右子树中最大的两个叶子节点之间的距离求出来,最终得到最 ...
- 测试单点登录xml配置
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...
- onclick 调用js选择器
- LOJ 2721 「NOI2018」屠龙勇士——扩展中国剩余定理
题目:https://loj.ac/problem/2721 1.注意别一输入 p[ i ] 就 a[ i ] %= p[ i ] ,因为在 multiset 里找的时候还需要真实值. 2.注意用 m ...
- selenium问题之定位不到元素(NoSuchElementException)
在使用selenium+爬虫的时候,经常会遇到一个问题,就是NoSuchElementException,定位不到元素的问题 一,打开了新页面,一般selenium点击新链接跳转打开了一个新页面,那么 ...