猴子补丁问题
#gevent使用-猴子补丁问题import time
import gevent
#解决猴子补丁问题,使用gevent都会有如下操作
from gevent import monkey
monkey.patch_all() #因为gevent模块有自己的sleep,对time模块的slepp不感知。这一步用猴子来偷换gevent模块的
def a():
for i in range(4):
print('A'+str(i))
time.sleep(0.1)
def b():
for i in range(4):
print('B'+str(i))
time.sleep(0.1)
def c():
for i in range(4):
print('C'+str(i))
time.sleep(0.1)
if __name__ == '__main__':
ga = gevent.spawn(a)
gb = gevent.spawn(b)
gc = gevent.spawn(c)
ga.join() #堵塞主进程
gb.join()
gc.join()
print('------------------')
我看出错是提前给猴子打补丁,错误提示如下
E:/python_pycharm/进阶学习/xiecheng 3.py:5: MonkeyPatchWarning: Monkey-patching ssl after ssl has already been imported may lead to errors, including RecursionError on Python 3.6. It may also silently lead to incorrect behaviour on Python 3.7. Please monkey-patch earlier. See https://github.com/gevent/gevent/issues/1016. Modules that had direct imports (NOT patched): ['_shaded_thriftpy.transport._ssl (D:\\办公\\pycharm\\PyCharm Community Edition 2020.1.1\\plugins\\python-ce\\helpers\\third_party\\thriftpy\\_shaded_thriftpy\\transport\\_ssl.py)'].
monkey.patch_all() #因为gevent模块有自己的sleep,对time模块的slepp不感知。这一步用猴子来偷换gevent模块的
A0
B0
C0
A1
B1
C1
A2
B2
C2
A3
B3
C3
------------------
Traceback (most recent call last):
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_code_executor.py", line 112, in add_exec
self.finish_exec(more)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_console_utils.py", line 210, in finish_exec
return server.notifyFinished(more)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_transport.py", line 226, in _req
return super(TSyncClient, self)._req(_api, *args, **kwargs)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 160, in _req
return self._recv(_api)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 172, in _recv
fname, mtype, rseqid = self._iprot.read_message_begin()
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\protocol\binary.py", line 372, in read_message_begin
self.trans, strict=self.strict_read)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\protocol\binary.py", line 164, in read_message_begin
sz = unpack_i32(inbuf.read(4))
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\transport\__init__.py", line 32, in read
return readall(self._read, sz)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\transport\__init__.py", line 14, in readall
chunk = read_fn(sz - have)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_transport.py", line 160, in _read
return self._reader.read_response(sz)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_transport.py", line 33, in read_response
return self._response_pipe.read(sz)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_io.py", line 40, in read
self.bytes_produced.wait()
File "D:\python3.7\Lib\threading.py", line 296, in wait
waiter.acquire()
File "D:\python3.7\lib\site-packages\gevent\thread.py", line 118, in acquire
acquired = BoundedSemaphore.acquire(self, blocking, timeout)
File "src\\gevent\\_semaphore.py", line 143, in gevent._gevent_c_semaphore.Semaphore.acquire
File "src\\gevent\\_semaphore.py", line 178, in gevent._gevent_c_semaphore.Semaphore.acquire
File "src\\gevent\\_abstract_linkable.py", line 381, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait
File "src\\gevent\\_abstract_linkable.py", line 346, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait_core
File "src\\gevent\\_abstract_linkable.py", line 348, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait_core
File "src\\gevent\\_abstract_linkable.py", line 303, in gevent._gevent_c_abstract_linkable.AbstractLinkable._AbstractLinkable__wait_to_be_notified
File "src\\gevent\\_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\\gevent\\_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\\gevent\\_greenlet_primitives.py", line 65, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\\gevent\\_gevent_c_greenlet_primitives.pxd", line 35, in gevent._gevent_c_greenlet_primitives._greenlet_switch
gevent.exceptions.LoopExit: This operation would block forever
Hub: <Hub '' at 0x1eb8b29a328 backend=default ptr=<cdata 'struct uv_loop_s *' 0x00007FFA5BC8BFA0> default pending=0 ref=0 thread_ident=0x5b50>
Handles:
[HandleState(handle=<cdata 'struct uv_handle_s *' 0x000001EB8A971A68>, type=b'check', watcher=<gevent.libuv.loop.loop at 0x1eb8af7a2b0 backend=default ptr=<cdata 'struct uv_loop_s *' 0x00007FFA5BC8BFA0> default pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x000001EB8A96D458>, type=b'timer', watcher=<gevent.libuv.loop.loop at 0x1eb8af7a2b0 backend=default ptr=<cdata 'struct uv_loop_s *' 0x00007FFA5BC8BFA0> default pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x000001EB8A971B18>, type=b'prepare', watcher=<gevent.libuv.loop.loop at 0x1eb8af7a2b0 backend=default ptr=<cdata 'struct uv_loop_s *' 0x00007FFA5BC8BFA0> default pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x000001EB8A9726C8>, type=b'check', watcher=<gevent.libuv.loop.loop at 0x1eb8af7a2b0 backend=default ptr=<cdata 'struct uv_loop_s *' 0x00007FFA5BC8BFA0> default pending=0 ref=0>, ref=1, active=0, closing=0)]
Traceback (most recent call last):
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_server.py", line 34, in handle
self.processor.process(iprot, oprot)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 257, in process
api, seqid, result, call = self.process_in(iprot)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 212, in process_in
api, type, seqid = iprot.read_message_begin()
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\protocol\binary.py", line 372, in read_message_begin
self.trans, strict=self.strict_read)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\protocol\binary.py", line 164, in read_message_begin
sz = unpack_i32(inbuf.read(4))
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\transport\__init__.py", line 32, in read
return readall(self._read, sz)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\transport\__init__.py", line 14, in readall
chunk = read_fn(sz - have)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\transport\buffered\__init__.py", line 39, in _read
self._rbuf = BytesIO(self._trans.read(max(sz, self._buf_size)))
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_transport.py", line 215, in read
return self._read_fn(sz)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_transport.py", line 27, in read_request
return self._request_pipe.read(sz)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_io.py", line 40, in read
self.bytes_produced.wait()
File "D:\python3.7\Lib\threading.py", line 296, in wait
waiter.acquire()
File "D:\python3.7\lib\site-packages\gevent\thread.py", line 118, in acquire
acquired = BoundedSemaphore.acquire(self, blocking, timeout)
File "src\\gevent\\_semaphore.py", line 143, in gevent._gevent_c_semaphore.Semaphore.acquire
File "src\\gevent\\_semaphore.py", line 178, in gevent._gevent_c_semaphore.Semaphore.acquire
File "src\\gevent\\_abstract_linkable.py", line 381, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait
File "src\\gevent\\_abstract_linkable.py", line 346, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait_core
File "src\\gevent\\_abstract_linkable.py", line 348, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait_core
File "src\\gevent\\_abstract_linkable.py", line 303, in gevent._gevent_c_abstract_linkable.AbstractLinkable._AbstractLinkable__wait_to_be_notified
File "src\\gevent\\_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\\gevent\\_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\\gevent\\_greenlet_primitives.py", line 65, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\\gevent\\_gevent_c_greenlet_primitives.pxd", line 35, in gevent._gevent_c_greenlet_primitives._greenlet_switch
gevent.exceptions.LoopExit: This operation would block forever
Hub: <Hub '' at 0x1eb8b29a608 backend=default ptr=<cdata 'struct uv_loop_s *' 0x000001EB8B1CFCE0> pending=0 ref=0 thread_ident=0x4c34>
Handles:
[HandleState(handle=<cdata 'struct uv_handle_s *' 0x000001EB8A972148>, type=b'check', watcher=<gevent.libuv.loop.loop at 0x1eb8b2aa240 backend=default ptr=<cdata 'struct uv_loop_s *' 0x000001EB8B1CFCE0> pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x000001EB8A96D5F8>, type=b'timer', watcher=<gevent.libuv.loop.loop at 0x1eb8b2aa240 backend=default ptr=<cdata 'struct uv_loop_s *' 0x000001EB8B1CFCE0> pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x000001EB8A972098>, type=b'prepare', watcher=<gevent.libuv.loop.loop at 0x1eb8b2aa240 backend=default ptr=<cdata 'struct uv_loop_s *' 0x000001EB8B1CFCE0> pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x000001EB8A971858>, type=b'check', watcher=<gevent.libuv.loop.loop at 0x1eb8b2aa240 backend=default ptr=<cdata 'struct uv_loop_s *' 0x000001EB8B1CFCE0> pending=0 ref=0>, ref=1, active=0, closing=0)]
没看懂请大佬指教
emmm,我没报错?
Twilight6 发表于 2020-6-11 18:51
emmm,我没报错?
#爬虫协程小例子
import urllib.request
import gevent
from gevent import monkey
monkey.patch_all()
def download(url): #下载网站源代码
response = urllib.request.urlopen(url)
html = response.read()
print('下载了{}数据,长度为{}'.format(url,len(html)))
if __name__ == '__main__':
urls = ['http://www.163.com','http://www.qq.com','http://www.4399.com']
#创建协程
g1 = gevent.spawn(download,urls)
g2 = gevent.spawn(download,urls)
g3 = gevent.spawn(download,urls)
#主进程与协程共死,所以要堵塞
gevent.joinall(g1,g2,g3)
print('---------------------')
也类似
E:/python_pycharm/进阶学习/xiecheng4.py:5: MonkeyPatchWarning: Monkey-patching ssl after ssl has already been imported may lead to errors, including RecursionError on Python 3.6. It may also silently lead to incorrect behaviour on Python 3.7. Please monkey-patch earlier. See https://github.com/gevent/gevent/issues/1016. Modules that had direct imports (NOT patched): ['_shaded_thriftpy.transport._ssl (D:\\办公\\pycharm\\PyCharm Community Edition 2020.1.1\\plugins\\python-ce\\helpers\\third_party\\thriftpy\\_shaded_thriftpy\\transport\\_ssl.py)'].
monkey.patch_all()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars)# execute the script
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "E:/python_pycharm/进阶学习/xiecheng4.py", line 20, in <module>
gevent.joinall(g1,g2,g3)
File "src\\gevent\\greenlet.py", line 1002, in gevent._gevent_cgreenlet.joinall
File "src\\gevent\\greenlet.py", line 1015, in gevent._gevent_cgreenlet.joinall
File "src\\gevent\\_hub_primitives.py", line 211, in gevent._gevent_c_hub_primitives.iwait_on_objects
File "src\\gevent\\_hub_primitives.py", line 247, in gevent._gevent_c_hub_primitives.iwait_on_objects
File "src\\gevent\\_hub_primitives.py", line 146, in gevent._gevent_c_hub_primitives._WaitIterator.__init__
TypeError: object of type 'gevent._gevent_cgreenlet.Greenlet' has no len()
Traceback (most recent call last):
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_server.py", line 34, in handle
self.processor.process(iprot, oprot)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 257, in process
api, seqid, result, call = self.process_in(iprot)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\thrift.py", line 212, in process_in
api, type, seqid = iprot.read_message_begin()
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\protocol\binary.py", line 372, in read_message_begin
self.trans, strict=self.strict_read)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\protocol\binary.py", line 164, in read_message_begin
sz = unpack_i32(inbuf.read(4))
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\transport\__init__.py", line 32, in read
return readall(self._read, sz)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\transport\__init__.py", line 14, in readall
chunk = read_fn(sz - have)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\third_party\thriftpy\_shaded_thriftpy\transport\buffered\__init__.py", line 39, in _read
self._rbuf = BytesIO(self._trans.read(max(sz, self._buf_size)))
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_transport.py", line 215, in read
return self._read_fn(sz)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_transport.py", line 27, in read_request
return self._request_pipe.read(sz)
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_comm\pydev_io.py", line 40, in read
self.bytes_produced.wait()
File "D:\python3.7\Lib\threading.py", line 296, in wait
waiter.acquire()
File "D:\python3.7\lib\site-packages\gevent\thread.py", line 118, in acquire
acquired = BoundedSemaphore.acquire(self, blocking, timeout)
File "src\\gevent\\_semaphore.py", line 143, in gevent._gevent_c_semaphore.Semaphore.acquire
File "src\\gevent\\_semaphore.py", line 178, in gevent._gevent_c_semaphore.Semaphore.acquire
File "src\\gevent\\_abstract_linkable.py", line 381, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait
File "src\\gevent\\_abstract_linkable.py", line 346, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait_core
File "src\\gevent\\_abstract_linkable.py", line 348, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait_core
File "src\\gevent\\_abstract_linkable.py", line 303, in gevent._gevent_c_abstract_linkable.AbstractLinkable._AbstractLinkable__wait_to_be_notified
File "src\\gevent\\_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\\gevent\\_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\\gevent\\_greenlet_primitives.py", line 65, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\\gevent\\_gevent_c_greenlet_primitives.pxd", line 35, in gevent._gevent_c_greenlet_primitives._greenlet_switch
gevent.exceptions.LoopExit: This operation would block forever
Hub: <Hub '' at 0x199c1fb58e8 backend=default ptr=<cdata 'struct uv_loop_s *' 0x00000199C20113D0> pending=0 ref=0 thread_ident=0x27f0>
Handles:
[HandleState(handle=<cdata 'struct uv_handle_s *' 0x00000199C0D86E78>, type=b'check', watcher=<gevent.libuv.loop.loop at 0x199c2177748 backend=default ptr=<cdata 'struct uv_loop_s *' 0x00000199C20113D0> pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x00000199BED31578>, type=b'timer', watcher=<gevent.libuv.loop.loop at 0x199c2177748 backend=default ptr=<cdata 'struct uv_loop_s *' 0x00000199C20113D0> pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x00000199C0D86378>, type=b'prepare', watcher=<gevent.libuv.loop.loop at 0x199c2177748 backend=default ptr=<cdata 'struct uv_loop_s *' 0x00000199C20113D0> pending=0 ref=0>, ref=0, active=1, closing=0),
HandleState(handle=<cdata 'struct uv_handle_s *' 0x00000199C0D86428>, type=b'check', watcher=<gevent.libuv.loop.loop at 0x199c2177748 backend=default ptr=<cdata 'struct uv_loop_s *' 0x00000199C20113D0> pending=0 ref=0>, ref=1, active=0, closing=0)]
下载了http://www.4399.com数据,长度为170375
下载了http://www.163.com数据,长度为495458
下载了http://www.qq.com数据,长度为51448
我跑到这个网站看 也没看懂 莫待无花空折枝 发表于 2020-6-11 19:03
也类似
我跑到这个网站看 也没看懂
是因为我的Pycharm少东西嘛? 莫待无花空折枝 发表于 2020-6-11 19:03
也类似
我跑到这个网站看 也没看懂
https://github.com/gevent/gevent/issues/1016 参数改成列表,正常运行,可能是你电脑环境问题,去重装试试?
https://fishc.com.cn/thread-162968-1-1.html
Twilight6 发表于 2020-6-11 19:15
参数改成列表,正常运行,可能是你电脑环境问题,去重装试试?
https://fishc.com.cn/thread-162968-1-1 ...
我按CSDN上面把关于genvet模块的都放在上面没解决
https://blog.csdn.net/a19990412/article/details/82966452
csdn上面几乎都是这个答案
我装了Anaconda也设置了conda环境 ,然后运行还是这样子 这个问题还没解决
大佬还有啥思路不? 莫待无花空折枝 发表于 2020-6-11 22:38
我按CSDN上面把关于genvet模块的都放在上面没解决
https://blog.csdn.net/a19990412/article/details/ ...
我也不懂了...重装试试吧? 莫待无花空折枝 发表于 2020-6-11 22:38
我按CSDN上面把关于genvet模块的都放在上面没解决
https://blog.csdn.net/a19990412/article/details/ ...
解决了?{:10_250:} Twilight6 发表于 2020-6-12 13:10
解决了?
没呢 还没想重装pycharm呢我先跳过把我的pycharm其他也没遇到过问题{:5_109:}
学到后面碰到在解决把
谢谢大佬 莫待无花空折枝 发表于 2020-6-12 14:07
没呢 还没想重装pycharm呢我先跳过把我的pycharm其他也没遇到过问题
学到后面碰到在 ...
客气了{:10_250:}
页:
[1]