报错:
BlockingIOError: [Errno 11] Resource temporarily unavailable
greenlet.error: cannot switch to a different thread
Exception in thread Thread-1:
Traceback (most recent call last):
File "/root/miniconda3/envs/fancp/lib/python3.6/site-packages/gevent/_socketcommon.py", line 722, in send
return self._sock.send(data, flags)
BlockingIOError: [Errno 11] Resource temporarily unavailable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/miniconda3/envs/fancp/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/root/miniconda3/envs/fancp/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "server2.py", line 43, in run
ws.send(js_data)
File "/root/miniconda3/envs/fancp/lib/python3.6/site-packages/geventwebsocket/websocket.py", line 344, in send
self.send_frame(message, opcode)
File "/root/miniconda3/envs/fancp/lib/python3.6/site-packages/geventwebsocket/websocket.py", line 328, in send_frame
self.raw_write(header + message)
File "/root/miniconda3/envs/fancp/lib/python3.6/site-packages/gevent/_socketcommon.py", line 699, in sendall
return _sendall(self, data_memory, flags)
File "/root/miniconda3/envs/fancp/lib/python3.6/site-packages/gevent/_socketcommon.py", line 409, in _sendall
timeleft = __send_chunk(socket, chunk, flags, timeleft, end)
File "/root/miniconda3/envs/fancp/lib/python3.6/site-packages/gevent/_socketcommon.py", line 338, in __send_chunk
data_sent += socket.send(chunk, flags)
File "/root/miniconda3/envs/fancp/lib/python3.6/site-packages/gevent/_socketcommon.py", line 727, in send
self._wait(self._write_event)
File "src/gevent/_hub_primitives.py", line 317, in gevent._gevent_c_hub_primitives.wait_on_socket
File "src/gevent/_hub_primitives.py", line 322, in gevent._gevent_c_hub_primitives.wait_on_socket
File "src/gevent/_hub_primitives.py", line 304, in gevent._gevent_c_hub_primitives._primitive_wait
File "src/gevent/_hub_primitives.py", line 46, in gevent._gevent_c_hub_primitives.WaitOperationsGreenlet.wait
File "src/gevent/_hub_primitives.py", line 46, in gevent._gevent_c_hub_primitives.WaitOperationsGreenlet.wait
File "src/gevent/_hub_primitives.py", line 55, in gevent._gevent_c_hub_primitives.WaitOperationsGreenlet.wait
File "src/gevent/_waiter.py", line 154, in gevent._gevent_c_waiter.Waiter.get
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
greenlet.error: cannot switch to a different thread
解决方法:
可参考:https://blog.csdn.net/pythontide/article/details/109242386
try:
sock.send('hello world!'.encode())
except BlockingIOError as err:
time.sleep(1)
sock.send('hello world!'.encode())
实际方法:
导致问题的原因是socket的ws.send(js_data)发送数据太频繁。
加入延时即可time.sleep(0.05)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)