我们都知道使用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协程之猴子补丁带来的坑的更多相关文章

  1. 协程gevent模块和猴子补丁

    # pip 装模块 greenlet和gevent # 协程 # 与进程.线程一样也是实现并发的手段 # 创建一个线程.关闭一个线程都需要创建寄存器.栈等.需要消耗时间 # 协程本质上是一个线程 # ...

  2. python协程初步--gevent库使用以及解释什么是猴子补丁monkey_patch

    协程工作的特点是遇到阻塞或耗时的任务时就切换,协程的生存依赖于线程,线程依赖于进程 一个似乎有点问题的例子 import gevent,time def kisscpc(num): for i in ...

  3. python并发编程之gevent协程(四)

    协程的含义就不再提,在py2和py3的早期版本中,python协程的主流实现方法是使用gevent模块.由于协程对于操作系统是无感知的,所以其切换需要程序员自己去完成. 系列文章 python并发编程 ...

  4. day38 各种队列、Event事件、协程、猴子补丁

    1.各种队列 我们已经学习了队列这种存取值的方法,我们以前使用的队列是可以进行进程间通信的(IPC),但是今天学习的这两种队列是不能进行进程间通信的,只能进行线程间的通信 这两种队列分别是先进后出式队 ...

  5. Gevent和猴子补丁

    定义 在2018年看Flutent python时了解到猴子补丁,知道咋回事,但是现在通过代码更深刻认识猴子补丁. 猴子补丁:在运行时修改类或模块,而不改动源码. 例子1 没有用猴子补丁 import ...

  6. Python之路-python(Queue队列、进程、Gevent协程、Select\Poll\Epoll异步IO与事件驱动)

    一.进程: 1.语法 2.进程间通讯 3.进程池 二.Gevent协程 三.Select\Poll\Epoll异步IO与事件驱动 一.进程: 1.语法 简单的启动线程语法 def run(name): ...

  7. python中的猴子补丁Monkey Patch

    python中的猴子补丁Monkey Patch 什么是猴子补丁 the term monkey patch only refers to dynamic modifications of a cla ...

  8. 什么是协程?与线程和进程对比优劣在哪?gevent协程示例代码

      协程 协程,又称微线程,纤程.英文名Coroutine..一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在 ...

  9. (20)gevent协程

    协程: 也叫纤程,协程是线程的一种实现,指的是一条线程能够在多任务之间来回切换的一 种实现,对于CPU.操作系统来说,协程并不存在 任务之间的切换会花费时间.目前电脑配置一般线程开到200会阻塞卡顿 ...

随机推荐

  1. winform 自定义控件属性在属性面板中显示

    Jan.David Nothing is impossible, the word itself says 'I'm possible'!" — Audrey Hepburn winform ...

  2. 【串线篇】Mybatis拓展之MBG

    MBG-逆向工程 一.介绍 MBG:MyBatis Generator:代码生成器: MyBatis官方提供的代码生成器:帮我们逆向生成: 正向: table----javaBean---BookDa ...

  3. C++ KMP文本匹配

    代码如下: 环境为VC #include <iostream> #include <algorithm> #include <string> #include &l ...

  4. zoj 3325 Machine(线段树)

    题意:0~n-1的数组,初始值为0:执行m个操作,每次操作执行后输出当前值为0的连续段的段数. 操作1: p i j : i~j区间的每个元素值减1 操作2: r i j :i~j区间的每个元素值加1 ...

  5. Halo(六)

    Spring publish-event 机制 监听者模式包含了一个监听者 Listener 与之对应的事件 Event,还有一个事件发布者 EventPublish. 过程就是 EventPubli ...

  6. modelviewset 中的序列化方法怎么用

  7. mybatis的Date类型。

    在写select的时候,里面的查询语句.where后面如果jdbcType=DATE没有写的话是 这个形式的. <select id="selectPhoto" parame ...

  8. 【Java】遍历Map<String,String>

    Map<String, String> map = new HashMap<>(); map.put("key1", "value1") ...

  9. && 和 || 逻辑运算符的短路运算

    &&和||的短路运算,是指如果在进行前面的表达式的运算过程,通过判断已经明确的知道整个表达式的结果,那么就不会进行后面表达式的运算判断. 表达式1 || 表达式2 || 表达式3... ...

  10. LG1440 求 m 区间内的最小值

    题目描述 一个含有 \(n\) 项的数列 (\(n≤ 2000000\)),求出每一项前的 \(m\) 个数到它这个区间内的最小值.若前面的数不足 \(m\) 项则从第 \(1\) 个数开始,若前面没 ...