在Windows 10 pro
上的venv
中运行。
我正在尝试按照Celery
和Flask
集成的教程进行操作:https://flask.palletsprojects.com/en/latest/patterns/celery/
# example.py
from celery import Celery, Task
from flask import Flask
def celery_init_app(app: Flask) -> Celery:
class FlaskTask(Task):
def __call__(self, *args: object, **kwargs: object) -> object:
with app.app_context():
return self.run(*args, **kwargs)
celery_app = Celery(app.name, task_cls=FlaskTask)
celery_app.config_from_object(app.config["CELERY"])
celery_app.set_default()
app.extensions["celery"] = celery_app
return celery_app
def create_app() -> Flask:
app = Flask(__name__)
app.config.from_mapping(
CELERY=dict(
# Redis Docker container connection string
broker_url="redis://default:redispw@localhost:55000",
result_backend="redis://default:redispw@localhost:55000",
task_ignore_result=True,
),
)
app.config.from_prefixed_env()
celery_init_app(app)
return app
# make_celery.py
from example import create_app
flask_app = create_app()
celery_app = flask_app.extensions["celery"]
命令行调用:celery -A make_celery worker --loglevel INFO
遇到:
Unrecoverable error: AttributeError("Can't pickle local object 'celery_init_app.<locals>.FlaskTask'")
[...]
.venv\lib\site-packages\billiard\reduction.py", line 123, in steal_handle
return _winapi.DuplicateHandle(
PermissionError: [WinError 5] Access is denied
同样的错误和追溯发生在示例repo上:https://github.com/pallets/flask/tree/main/examples/celery
我该如何解决这个问题?有哪些替代方案可用?
尝试将class FlaskTask(Task)
移出celery_init_app
函数的局部作用域,但显然我失去了对app
变量的访问权限。
1条答案
按热度按时间fwzugrvs1#
我遇到了同样的问题。将celepie_init_app更改为这样的阅读方式修复了它
我希望我能给予一个解释的“为什么”。如果有人知道,请张贴它