python - greenlet.error: cannot switch to a different thread
问题描述
I have a Flask application, getting this error while trying to integrate flask with faust.
app.py
import mode.loop.eventlet
import logging
import logging.config
import json
from flask import Flask
from elasticapm.contrib.flask import ElasticAPM
def create_app():
app = Flask(__name__)
configure_apm(app)
configure_logging()
register_blueprints(app)
register_commands(app)
return app
main.py
from flask import jsonify
from litmus.app import create_app
from intercepter import Intercepter
app = create_app()
app.wsgi_app = Intercepter(app.wsgi_app , app)
@app.route('/status')
def status():
return jsonify({'status': 'online'}), 200
another controller
@api_blue_print.route('/v1/analyse', methods=['POST'])
def analyse():
analyse_with_historic_data.send(value=[somedata])
return jsonify({'message': 'Enqueued'}), 201
analyse_with_historic_data.py
@app.agent(analysis_topic)
async def analyse_with_historic_data(self, stream):
async for op in stream:
entity_log = EntityLog.where('id', op.entity_log_id).first()
Error Trace:
Traceback (most recent call last):
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 461, in fire_timers
timer()
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/hubs/timer.py", line 59, in __call__
cb(*args, **kw)
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/semaphore.py", line 147, in _do_acquire
waiter.switch()
greenlet.error: cannot switch to a different thread
Traceback (most recent call last):
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 461, in fire_timers
timer()
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/hubs/timer.py", line 59, in __call__
cb(*args, **kw)
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/semaphore.py", line 147, in _do_acquire
waiter.switch()
greenlet.error: cannot switch to a different thread
Traceback (most recent call last):
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/queue.py", line 118, in switch
self.greenlet.switch(value)
greenlet.error: cannot switch to a different thread
^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/threading.py", line 551, in wait
signaled = self._cond.wait(timeout)
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/threading.py", line 299, in wait
gotit = waiter.acquire(True, timeout)
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/semaphore.py", line 107, in acquire
hubs.get_hub().switch()
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 298, in switch
return self.greenlet.switch()
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 350, in run
self.wait(sleep_time)
File "/Users/sahilpaudel/.pyenv/versions/3.6.5/lib/python3.6/site-packages/eventlet/hubs/kqueue.py", line 96, in wait
time.sleep(seconds)
I have trying to fix this issue by monkey.patch_all but that too it didn't work out giving another stacktrace that lock cannot be released something.
解决方案
当我尝试使用 Pycharm 调试烧瓶应用程序时,发生了类似的事情。
我最终为最终解决我的问题所做的是在 Pycharm 中启用 gevent 兼容性:
文件 -> 设置 -> 构建、执行、部署 -> Python 调试器 -> Gevent 兼容
推荐阅读
- hyperledger-fabric - hurley 生成的 Hyperledger 网络
- python - 对 netCDF 文件的变量中的每个值执行计算
- python - 获取 pandas.MultiIndex 中关卡的位置
- mysql - 计算两个日期之间 mysql 表中的记录,如果行为空,则返回零
- javascript - JS / Clojure(或任何)代码用于将极坐标转换为(切线?)正确矩形上的笛卡尔坐标?
- c++ - 除了使用 set 之外,是否可以为 CMake SOURCE(S) 设置值?
- java - MS 最新版本的 JDBC 驱动程序正在使用(已弃用)ADAL 库。MSAL 的计划是什么?
- angular - 值更改时处理禁用/启用表单
- javascript - Vue.js - 在计算中使用本地方法是不好的做法吗?
- docker - ROS的Docker镜像无法使用?