grequests 出现 greenlet.error: cannot switch to a different thread
使用 Python grequests 库出现:greenlet.error: cannot switch to a different thread
题目来源及自己的思路
在 Django 项目中使用 grequests 库替代 requests 库并发请求,出现
greenlet.error: cannot switch to a different thread。对项目正常使用没有什么很大影响。
网上查询相关错误,发现有个哥们用 Flask 时碰到过,但是 flask 与 Django 还是有点不一样的。
参考:https://blog.csdn.net/A156348...
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
import grequestsdef async_http(peer_list):
headers = {
# 'Connection': 'close',
'Content-Type': "application/x-www-form-urlencoded",
'cache-control': "no-cache",
'Postman-Token': "d2d27edd-6795-45be-a1ef-d4cd749f79c4"
}
print('进入 async_http 函数》》》》')
start_time = time.time()
timestamp = create_timestamp()
url_list = []
for peer in peer_list:
befor = "get_tunnel_info?AccessId=vpncnadmin&peer=%s×tamp=%s" % (peer, timestamp)
sign = get_sign(befor)
url = "http://xxxx/api/get_tunnel_info?AccessId=vpncnadmin&peer=" + peer + "×tamp=" + timestamp + "&sign=" \
+ sign
url_list.append(url)
task = [grequests.get(url, headers=headers, timeout=30) for url in url_list]
response = grequests.map(task, exception_handler=exception_handler, size=50)
end_time = time.time()
print('总耗时:', end_time - start_time)
logger().info('查询结束,总共 %s 条 peer,总耗时:%s' % (len(peer_list), end_time - start_time))
return response
def exception_handler(request, exception):
"""单个请求错误提示"""
print('请求错误', request, exception)
failed_peer_list = [] # 有问题的 peer 列表
send_url = request.url
peer = send_url.split('&')[1].split('=')[1]
failed_peer_list.append(peer)
return failed_peer_list
你期待的结果是什么?实际看到的错误信息又是什么?
这是具体的错误信息:
----------------------------------------Exception happened during processing of request from ('127.0.0.1', 51948)
Traceback (most recent call last):
File "c:\Python36\Lib\socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "c:\Python36\Lib\socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "c:\Python36\Lib\socketserver.py", line 724, in __init__
self.handle()
File "D:\pycharm resource\Projects\virtual\env-py36\lib\site-packages\django\core\servers\basehttp.py", line 139, in handle
self.raw_requestline = self.rfile.readline(65537)
File "c:\Python36\Lib\socket.py", line 586, in readinto
return self._sock.recv_into(b)
File "D:\pycharm resource\Projects\virtual\env-py36\lib\site-packages\gevent\_socket3.py", line 433, in recv_into
self._wait(self._read_event)
File "src\gevent\_hub_primitives.py", line 284, in gevent.__hub_primitives.wait_on_socket
File "src\gevent\_hub_primitives.py", line 289, in gevent.__hub_primitives.wait_on_socket
File "src\gevent\_hub_primitives.py", line 271, in gevent.__hub_primitives._primitive_wait
File "src\gevent\_hub_primitives.py", line 46, in gevent.__hub_primitives.WaitOperationsGreenlet.wait
File "src\gevent\_hub_primitives.py", line 46, in gevent.__hub_primitives.WaitOperationsGreenlet.wait
File "src\gevent\_hub_primitives.py", line 55, in gevent.__hub_primitives.WaitOperationsGreenlet.wait
File "src\gevent\_waiter.py", line 151, in gevent.__waiter.Waiter.get
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
greenlet.error: cannot switch to a different thread
----------------------------------------
[16/Oct/2019 11:13:43] "GET /app/v1/devices/ HTTP/1.1" 200 16016
[16/Oct/2019 11:13:43] "GET /static/js/devices_list.js HTTP/1.1" 304 0
回答:
使用环境 Windows
回答:
请问您解决了嘛?我也遇到了一样的问题
以上是 grequests 出现 greenlet.error: cannot switch to a different thread 的全部内容, 来源链接: utcz.com/a/161623.html