我们都知道使用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, '20191022', 0, 0, '1068', 1, 1, 0, 0), (1, '20181022', 0, 0, '1068', 1, 1, 0, 0), (2, '20171022', 0, 0, '1068', 1, 1, 0, 0), (3, '20161022', 0, 0, '1068', 1, 1, 0, 0), (4, '20151022', 0, 0, '1068', 1, 1, 0, 0), (5, '20141022', 0, 0, '738', 1, 1, 0, 0), (6, '20131022', 0, 0, '738', 1, 1, 0, 0), (7, '20121022', 0, 0, '738', 1, 1, 0, 0), (8, '20111022', 0, 0, '738', 1, 1, 0, 0), (9, '20101022', 0, 0, '738', 1, 1, 0, 0), (10, '20091022', 0, 0, '738', 1, 1, 0, 0), (11, '20081022', 0, 0, '628', 1, 1, 0, 0), (12, '20071022', 0, 0, '628', 1, 1, 0, 0), (13, '20061022', 0, 0, '628', 1, 1, 0, 0), (14, '20051022', 0, 0, '628', 1, 1, 0, 0), (15, '20041022', 0, 0, '628', 1, 1, 0, 0), (16, '20031022', 0, 0, '628', 1, 1, 0, 0), (17, '20021022', 0, 0, '628', 1, 1, 0, 0), (0, '20191022', 0, 0, '2548', 0, 1, 0, 0), (1, '20181022', 0, 0, '2548', 0, 1, 0, 0), (2, '20171022', 0, 0, '2548', 0, 1, 0, 0), (3, '20161022', 0, 0, '2548', 0, 1, 0, 0), (4, '20151022', 0, 0, '2548', 0, 1, 0, 0), (5, '20141022', 0, 0, '1698', 0, 1, 0, 0), (6, '20131022', 0, 0, '1698', 0, 1, 0, 0), (7, '20121022', 0, 0, '1698', 0, 1, 0, 0), (8, '20111022', 0, 0, '1698', 0, 1, 0, 0), (9, '20101022', 0, 0, '1698', 0, 1, 0, 0), (10, '20091022', 0, 0, '1698', 0, 1, 0, 0), (11, '20081022', 0, 0, '1268', 0, 1, 0, 0), (12, '20071022', 0, 0, '1268', 0, 1, 0, 0), (13, '20061022', 0, 0, '1268', 0, 1, 0, 0), (14, '20051022', 0, 0, '1268', 0, 1, 0, 0), (15, '20041022', 0, 0, '1268', 0, 1, 0, 0), (16, '20031022', 0, 0, '1268', 0, 1, 0, 0), (17, '20021022', 0, 0, '1268', 0, 1, 0, 0), (0, '20191022', 1, 1, '98', 1, 2, 0, 0), (1, '20181022', 1, 1, '98', 1, 2, 0, 0), (2, '20171022', 1, 1, '98', 1, 2, 0, 0), (3, '20161022', 1, 1, '98', 1, 2, 0, 0), (4, '20151022', 1, 1, '98', 1, 2, 0, 0), (5, '20141022', 1, 1, '68', 1, 2, 0, 0), (6, '20131022', 1, 1, '68', 1, 2, 0, 0), (7, '20121022', 1, 1, '68', 1, 2, 0, 0), (8, '20111022', 1, 1, '68', 1, 2, 0, 0), (9, '20101022', 1, 1, '68', 1, 2, 0, 0), (10, '20091022', 1, 1, '68', 1, 2, 0, 0), (11, '20081022', 1, 1, '58', 1, 2, 0, 0), (12, '20071022', 1, 1, '58', 1, 2, 0, 0), (13, '20061022', 1, 1, '58', 1, 2, 0, 0), (14, '20051022', 1, 1, '58', 1, 2, 0, 0), (15, '20041022', 1, 1, '58', 1, 2, 0, 0), (16, '20031022', 1, 1, '58', 1, 2, 0, 0), (17, '20021022', 1, 1, '58', 1, 2, 0, 0), (0, '20191022', 1, 1, '238', 0, 2, 0, 0), (1, '20181022', 1, 1, '238', 0, 2, 0, 0), (2, '20171022', 1, 1, '238', 0, 2, 0, 0), (3, '20161022', 1, 1, '238', 0, 2, 0, 0), (4, '20151022', 1, 1, '238', 0, 2, 0, 0), (5, '20141022', 1, 1, '158', 0, 2, 0, 0), (6, '20131022', 1, 1, '158', 0, 2, 0, 0), (7, '20121022', 1, 1, '158', 0, 2, 0, 0), (8, '20111022', 1, 1, '158', 0, 2, 0, 0), (9, '20101022', 1, 1, '158', 0, 2, 0, 0), (10, '20091022', 1, 1, '158', 0, 2, 0, 0), (11, '20081022', 1, 1, '118', 0, 2, 0, 0), (12, '20071022', 1, 1, '118', 0, 2, 0, 0), (13, '20061022', 1, 1, '118', 0, 2, 0, 0), (14, '20051022', 1, 1, '118', 0, 2, 0, 0), (15, '20041022', 1, 1, '118', 0, 2, 0, 0), (16, '20031022', 1, 1, '118', 0, 2, 0, 0), (17, '20021022', 1, 1, '118', 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