我们都知道使用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. (ACM模板)映射map

    #include<iostream> #include<cstdio> #include<map> using namespace std; int main() ...

  2. 几种IO机制区别

    IO的方式通常分为几种,同步阻塞的BIO.同步非阻塞的NIO.异步非阻塞的AIO. 一.BIO 在JDK1.4出来之前,我们建立网络连接的时候采用BIO模式,需要先在服务端启动一个ServerSock ...

  3. Linux centos7安装git

    1.下载git wget https://github.com/git/git/archive/v2.14.1.zip 2.安装依赖 yum -y install zlib-devel openssl ...

  4. thread 类详解

    java.lang.Thread类详解 一.前言 位于java.lang包下的Thread类是非常重要的线程类,它实现了Runnable接口,今天我们来学习一下Thread类,在学习Thread类之前 ...

  5. Shell输入命令时一些有用的快捷键

    Ctrl + u: 从光标所在位置一直删除到开头 Ctrl + k: 从光标所在位置一直删除到尾 Ctrl + b: 光标向后移动一个字符 Ctrl + f: 光标后前移动一个字符 Alt + b: ...

  6. Oracle中表连接的运行原理

    Oracle优化器会自动选择以下三种方式的一种运行表连接,但在数据环境上配合强化选择合适的方式或强制使用某种方式是SQL优化的需要:      NESTED LOOP 对于被连接的数据子集较小的情况, ...

  7. FMXUI TEXTVIEW代码设置IMAGEINDEX

    FMXUI作为一个开源的控件,真是DELPHIER的福音,向作者致敬.​TEXTVIEW非常好用,在属性面板中有ImageIndex属性,可以方便设置图标,在实际应用中图标状态需要改变,但在代码设置时 ...

  8. HttpClient之EntityUtils工具类

    今天看到tttpclient-tutorial上面有这样一句话-----非常的不推荐使用EntityUtils,除非知道Entity是来自可信任的Http Server 而且还需要知道它的最大长度.文 ...

  9. PHP curl_exec函数

    curl_exec — 执行一个cURL会话 说明 mixed curl_exec ( resource $ch ) 执行给定的cURL会话. 这个函数应该在初始化一个cURL会话并且全部的选项都被设 ...

  10. Android逆向之旅---Android应用的汉化功能(修改SO中的字符串内容)

    一.前言 今天我们继续来讲述逆向的知识,今天我们来讲什么呢?我们在前一篇文章中介绍了关于SO文件的格式,今天我们继续这个话题来看看如何修改SO文件中的内容,看一下我们研究的主题: 需求:想汉化一个Ap ...