Celery:任务已注册但未从django触发

uinbv5nw  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(121)

一月一日

from .celery import app as celery_app
__all__ = ['celery_app']

一个月一个月

from __future__ import absolute_import
import os

from celery import Celery
from celery.schedules import crontab
from django.conf import settings

# set the default Django settings module for the 'celery' program.

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tax.settings')
app = Celery('tax')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks()

settings.py

BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT = ['application/json', 'json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kathmandu'
CELERY_IMPORTS = ('tax.tasks',)
CELERY_ENABLE_UTC = False

Python和Celery版本:

Python 3.8.10
flower==0.9.7
celery==4.3.0

**pip freeze**命令结果:

aioredis==1.3.1
amqp==2.6.1
apns2==0.7.2
arabic-reshaper==2.1.3
asgiref==3.3.1
asn1crypto==1.5.1
async-timeout==3.0.1
attrs==20.3.0
autobahn==21.3.1
Automat==20.2.0
Babel==2.2.0
backcall==0.2.0
backports.zoneinfo==0.2.1
billiard==3.6.4.0
celery==4.3.0
certifi==2020.12.5
cffi==1.14.5
channels==3.0.3
channels-redis==3.2.0
chardet==4.0.0
click==8.1.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
constantly==15.1.0
coverage==6.2
cryptography==3.4.6
cssselect2==0.6.0
daphne==3.0.1
decorator==4.4.2
defusedxml==0.7.1
diff-match-patch==20200713
Django==3.1.7
django-allauth==0.44.0
django-anymail==8.6
django-cachalot==2.3.3
django-cors-headers==3.7.0
django-debug-toolbar==3.2
django-extensions==3.1.1
django-filter==2.4.0
django-grappelli==2.14.3
django-import-export==2.5.0
django-js-asset==1.2.2
django-mptt==0.12.0
django-nested-admin==3.3.3
django-phonenumber-field==5.0.0
django-push-notifications==2.0.0
django-rest-auth==0.9.5
django-rest-framework==0.1.0
django-seed==0.3.1
django-summernote==0.8.11.6
django-webpush==0.3.3
djangorestframework==3.12.2
drf-spectacular==0.22.1
et-xmlfile==1.0.1
factory-boy==3.2.1
Faker==9.9.0
flower==0.9.7
future==0.18.2
gunicorn==20.0.4
h2==2.6.2
hiredis==1.1.0
hpack==3.0.0
html5lib==1.1
http-ece==1.1.0
humanize==4.2.3
hyper==0.7.0
hyperframe==3.2.0
hyperlink==21.0.0
idna==2.10
importlib-resources==5.7.1
incremental==21.3.0
inflection==0.5.1
iniconfig==1.1.1
ipdb==0.13.5
ipython==7.21.0
ipython-genutils==0.2.0
jedi==0.18.0
jsonschema==4.5.1
kombu==4.6.11
lxml==4.8.0
Markdown==3.3.4
MarkupPy==1.14
msgpack==1.0.2
numpy==1.21.4
oauthlib==3.1.0
odfpy==1.4.1
opencv-python==4.5.4.60
openpyxl==3.0.7
oscrypto==1.3.0
packaging==21.3
parso==0.8.1
pdfrw==0.4
pexpect==4.8.0
phonenumbers==8.12.19
pickleshare==0.7.5
Pillow==8.1.1
pluggy==1.0.0
prometheus-client==0.8.0
prompt-toolkit==3.0.16
ptyprocess==0.7.0
py==1.11.0
py-vapid==1.7.1
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
Pygments==2.8.0
pyHanko==0.13.1
pyhanko-certvalidator==0.19.5
PyJWT==1.7.1
pyOpenSSL==20.0.1
pyparsing==3.0.6
PyPDF2==1.27.12
PyPDF3==1.0.6
pyrsistent==0.18.1
pytest==6.2.5
pytest-cov==3.0.0
pytest-django==4.5.1
pytest-spec==3.2.0
pytest-testdox==2.0.1
python-bidi==0.4.2
python-dateutil==2.8.2
python-dotenv==0.20.0
python-monkey-business==1.0.0
python3-openid==3.2.0
pytz==2015.7
pytz-deprecation-shim==0.1.0.post0
pywebpush==1.9.4
PyYAML==5.4.1
qrcode==7.3.1
redis==3.5.3
reportlab==3.6.9
requests==2.25.1
requests-oauthlib==1.3.0
sentry-sdk==1.5.3
service-identity==18.1.0
six==1.15.0
sqlparse==0.4.1
svglib==1.3.0
sweetify==2.3.1
tablib==3.0.0
text-unidecode==1.3
tinycss2==1.1.1
toml==0.10.2
tomli==1.2.2
toposort==1.7
tornado==6.2
tqdm==4.64.0
traitlets==5.0.5
Twisted==21.2.0
txaio==21.2.1
tzdata==2022.1
tzlocal==4.2
uritemplate==4.1.1
uritools==4.0.0
urllib3==1.26.3
vine==1.3.0
wcwidth==0.2.5
webencodings==0.5.1
Werkzeug==2.0.2
xhtml2pdf==0.2.7
xlrd==2.0.1
xlwt==1.3.0
zipp==3.8.0
zope.interface==5.2.0

tasks.py

from celery import shared_task
@shared_task
def click_example(event_id):
    print(f'working celery example {event_id}')

./manage.py shell中:

In [-]: from taxnepal.tasks import click_example

In [-]: click_example.delay('18')

celery -A tax worker -l info结果:

/root/env/lib/python3.8/site-packages/celery/platforms.py:800: RuntimeWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!

Please specify a different user using the --uid option.

User information: uid=0 euid=0 gid=0 egid=0

    warnings.warn(RuntimeWarning(ROOT_DISCOURAGED.format(
    
    -------------- celery@ubuntu-s-1vcpu-1gb-nyc1-01 v4.3.0 (rhubarb)
---- **** ----- 
--- * ***  * -- Linux-5.4.0-67-generic-x86_64-with-glibc2.29 2022-07-10 15:14:37
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         taxnepal:0x7f7954c36220
- ** ---------- .> transport:   redis://127.0.0.1:6379//
- ** ---------- .> results:     redis://127.0.0.1:6379/
- *** --- * --- .> concurrency: 1 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
    -------------- [queues]
                .> celery           exchange=celery(direct) key=celery
                

[tasks]
    . remove_phone_otp
    . send_notification
    . taxnepal.tasks.click_example
    . taxnepal.tasks.generate_event_registree_certificate

[2022-07-10 15:14:37,357: INFO/MainProcess] Connected to redis://127.0.0.1:6379//
[2022-07-10 15:14:37,366: INFO/MainProcess] mingle: searching for neighbors
[2022-07-10 15:14:38,388: INFO/MainProcess] mingle: all alone
[2022-07-10 15:14:38,402: WARNING/MainProcess] /root/env/lib/python3.8/site-packages/celery/fixups/django.py:202: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
    warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2022-07-10 15:14:38,403: INFO/MainProcess] celery@ubuntu-s-1vcpu-1gb-nyc1-01 ready.

celery -A tax beat -l INFO结果:

celery beat v4.3.0 (rhubarb) is starting.
__    -    ... __   -        _
LocalTime -> 2022-07-10 15:19:36
Configuration ->
    . broker -> redis://127.0.0.1:6379//
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . logfile -> [stderr]@%INFO
    . maxinterval -> 5.00 minutes (300s)
[2022-07-10 15:19:36,070: INFO/MainProcess] beat: Starting...

无法从shell和Django视图运行任务。任务挂起直到超时。它在开发服务器上工作,但在生产服务器上不工作。什么问题我已经遵循了所有的指南,并非常感谢帮助。

aydmsdu9

aydmsdu91#

我用下面的实现解决了这个问题。
celery.py为单位

app = Celery('tax', broker=BROKER_URL)

@app.task
def click_example(event_id):
    print(f'working celery example {event_id}')

并从视图或需要触发任务的任何位置导入任务。
参考编号:
Distributed Task Queue explained for beginners to Professionals(Part-1)

相关问题