工作以外的应用程序方面的瓶芹菜

0

的问题

我将前言,我知道有几个方案为这一问题的浮动,虽然他们没有按照本指南 在这里 ,似乎没有回答我的问题。

我收到这个错误我芹菜应用在烧瓶。

[2021-11-23 22:50:01,469: ERROR/ForkPoolWorker-1] Task smartful_versioning_flask.test_celery_task[6dda4a4b-cf64-4fdc-8317-237dca9ad31b] raised unexpected: RuntimeError('Working outside of application context.\n\nThis typically means that you attempted to use functionality that needed\nto interface with the current application object in some way. To solve\nthis, set up an application context with app.app_context().  See the\ndocumentation for more information.')
Traceback (most recent call last):
  File "/var/smartful_flask/smartful_venv/lib/python3.7/site-packages/celery/app/trace.py", line 450, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/var/smartful_flask/smartful_venv/lib/python3.7/site-packages/celery/app/trace.py", line 731, in __protected_call__
    return self.run(*args, **kwargs)
  File "/var/smartful_flask/smartful_versioning_flask.py", line 21, in test_celery_task
    return jsonify({"something": "something"})
  File "/var/smartful_flask/smartful_venv/lib/python3.7/site-packages/flask/json/__init__.py", line 336, in jsonify
    if current_app.config["JSONIFY_PRETTYPRINT_REGULAR"] or current_app.debug:
  File "/var/smartful_flask/smartful_venv/lib/python3.7/site-packages/werkzeug/local.py", line 422, in __get__
    obj = instance._get_current_object()
  File "/var/smartful_flask/smartful_venv/lib/python3.7/site-packages/werkzeug/local.py", line 544, in _get_current_object
    return self.__local()  # type: ignore
  File "/var/smartful_flask/smartful_venv/lib/python3.7/site-packages/flask/globals.py", line 47, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
**RuntimeError: Working outside of application context.**

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.

之后多的疑难解答,我在一个完整的损失。 其中没有一个通过在程序上下文中引用的例外?


     from flask import Flask, jsonify
     from celery import Celery
     
     app = Flask(__name__)
     app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
     app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
     
     celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
     celery.conf.update(app.config)
     
     @celery.task
     def test_celery_task():
         return jsonify({"something": "something"})
     
     @app.route("/web/start/backend", methods=['POST'])
     def start_task():
         task = test_celery_task.apply_async()
         return task.id
     
     @app.route("/web/check-status/backend/<task_id>", methods=['GET'])
     def check_task(task_id):
         task = test_celery_task.AsyncResult(task_id)
     
         if task.state == 'PENDING':
             return jsonify({
                 "task": task_id,
                 "state": task.state,
                 "data": None,
                 "complete": False,
                 "error": False
             })
         elif task.state == 'SUCCESS':
             print(task.info)
         else:
             print(task.state)
     
         return "task.info"

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

预期在这里,带这回溯,该任务将会失败,并未返回后台。 我使用Ngnix,过,主管服务于这个--虽然相同的问题存在有或没有监产卵的工人。

我相信这是什么愚蠢的和平凡,我俯瞰着,虽然如往常一样-太盲目到看到它。

celery flask python
2021-11-23 23:15:00
1

最好的答案

0

我已经找到了解决在这里,我记录为其他类似的情况。 这已经做的工作目录,主管使用。

我走进录我的瓶中的应用程序,并产生了一个单一的工作人员手动:

# smartful_venv/bin/celery --app=smartful_versioning_flask.celery worker --loglevel=DEBUG

我跑我的瓶中的应用程序中可以看到的问题,我返回印成功。 我的情况是有可能的主管没有访问的工作目录我的使用。 这不是明确说明,但是这个问题不是讨论的主管docs真的,虽然监督报告,它正在运行为的根它的谎言!

编辑要添加:

在另一个任务,我需要增加 with app.app_context(): 之前做什么。 这就是奇怪我,但它的工作。 如果任何人有解释为什么应用程序方面存在的唯一之后,这可能会导致一个健康的讨论。

2021-11-24 19:09:10

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................