首页 > 解决方案 > uwsgi + flask + python print 在 request.get() 后停止工作

问题描述

尝试从控制台使用 uwsgi 记录 python 打印时遇到问题。

所以我从控制台运行我的应用程序:

uwsgi --http :9090 --wsgi-file wsgi.py --master -p 4

我的 wsgy.py 文件包含:

from assets_generator import app as application

if __name__ == "__main__":
    app.run()

我的应用程序看起来像这样(在asset_generator.py 中):

from flask import Flask, render_template
app = Flask(__name__)

app.config.from_envvar('CONFIG')

from uwsgidecorators import thread
from worker import Worker

@thread
def _start_worker(item):
    worker = Worker(item=item)
    worker.run()

@app.route("/post-asset", methods=['GET', 'POST'])
def post_asset():
    from flask import request
    _start_worker(request.values)
    return "OK", 200

worker 的 run 类调用了一个 convert 方法:

class Worker(object):
    def __init__(self, item):
        super(Worker, self).__init__()
        self.item = item

    def run(self):
        with app.app_context():
            # prepare stuff for convertion, fill urls etc....
            details = self.convert(
                name=self.item.get('name'),
                source_url=self.item.get('source_url'),
                conversion_format=self.item.get('format'),
                default_options=default_options
            )

并且转换方法调用一个url:

def convert(self, name, source_url, conversion_format, default_options):

try:
    print "before requests " + source_url # THIS PRINT WORKS
    r = requests.get(source_url)  
    print "after requests"                # THIS ONE DOESN'T

    # do other stuff, prints doesn't work

except Exception as e:
   print " Error"
   raise e
finally:
   print "finally"                        # DOESN'T PRINT
   if zip_extract_path:
      shutil.rmtree(zip_extract_path)

print "before returning None"             # DOESN'T PRINT
return None

我的问题是我可以在 uwsgi 控制台日志中看到第一个打印,但第二个实际上从未发生过,并且在此请求调用之后的任何其他打印都不会发生。

我已经手动测试过

r = requests.get(source_url)

使用来自运行此 uwsgi 应用程序的位置的正确 url,请求实际上成功并返回 OK。

我有点困惑为什么我的打印停止工作,如果有人对此有见解,将不胜感激。

标签: pythonflaskpython-requestsuwsgi

解决方案


出于某种原因,在尝试打印一个不存在的变量并--py-autoreload 1在 uwsgi 配置中进行设置后,现在显示了我的日志,但我不明白为什么。


推荐阅读