grequests 出现 greenlet.error: cannot switch to a different thread

发布于 2022-09-11 22:57:40 字数 4221 浏览 21 评论 0

使用 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 grequests

def 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&timestamp=%s" % (peer, timestamp)
        sign = get_sign(befor)

        url = "http://xxxx/api/get_tunnel_info?AccessId=vpncnadmin&peer=" + peer + "&timestamp=" + 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚风撩人 2022-09-18 22:57:40

使用环境 Windows

抱猫软卧 2022-09-18 22:57:40

请问您解决了嘛?我也遇到了一样的问题

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文