我有一个Flask应用程序,在尝试将Flask与faust集成时出错。
应用程序
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
主.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
另一个控制器
@api_blue_print.route('/v1/analyse', methods=['POST'])
def analyse():
analyse_with_historic_data.send(value=[somedata])
return jsonify({'message': 'Enqueued'}), 201
使用分析历史数据
@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()
错误跟踪:
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)
我试图通过monkey.patch_all来解决这个问题,但也没有成功,给了另一个无法释放锁的堆栈。
当我尝试使用Pycharm调试一个flask应用程序时,也发生了类似的事情。
我最终解决了我的问题,在Pycharm中启用了gevent兼容性:
File->设置->生成、执行、部署->Python调试器->Gevent兼容
如果前面的答案没有帮助,请在所有导入之前粘贴以下内容:
from gevent import monkey
monkey.patch_all()
请参见此处:https://www.gevent.org/api/gevent.monkey.html