from myapp.celery import app as celery_app
celery_app.worker_main(argv=['worker', '--loglevel=debug', '-E', '-P', 'eventlet'])
用他启动会显示
[2024-06-06 11:28:27,149: INFO/MainProcess] Task home_application.tasks.handle_query_task[fc8f1d5e-f785-4f5e-bab8-73ba23c81217] received
[2024-06-06 11:28:27,150: DEBUG/MainProcess] TaskPool: Apply (args:('home_application.tasks.handle_query_task', 'fc8f1d5e-f785-4f5e-bab8-73ba23c81217'
就卡住了。
但是用celery -A myapp worker --loglevel=debug -E -P eventlet启动,就能正常处理工作。
app/init.py
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ('celery_app',)
app/celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from myapp import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
app = Celery('myapp')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
home_application/tasks.py
@app.task
def handle_query_task(task_id, *args, **kwargs):
return {"status": 1, "error": "test"}