django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 19767205568
00 and this is thread id 1976775359680.
问题:
执行celery worker -A s1 -l info -P eventlet能正常,放立即执行的任务(delay)没有问题,不过放apply_async的任务时就会报上面的错误。
网上都说是版本的问题,我的版本是
celery 4.4.0
Django 4.2.1
django-redis 5.2.0
eventlet 0.33.3
redis 4.5.4
解决办法:
由于-P eventlet是多线程任务的,改成单线程任务,用参数--pool=solo代替-P eventlet。
还需要把python3.9换成python3.8才可以,否则还会报其它错。
当前包的版本信息如下:文章来源:https://www.toymoban.com/news/detail-513578.html
celery 4.4.0
Django 4.2.2
django-redis 5.3.0
eventlet 0.33.3
redis 4.5.4
执行这个就正常了 文章来源地址https://www.toymoban.com/news/detail-513578.html
celery worker -A djangoTest -l info --pool=solo
(venv38) D:\PYProject\djangoTest>celery worker -A djangoTest -l info --pool=solo
-------------- celery@QHT81 v4.4.0 (cliffs)
--- ***** -----
-- ******* ---- Windows-10-10.0.18362-SP0 2023-06-28 11:20:33
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: demos:0x1d462de8850
- ** ---------- .> transport: redis://127.0.0.1:6379//
- ** ---------- .> results: redis://127.0.0.1:6379/
- *** --- * --- .> concurrency: 12 (solo)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. api.tasks.s1
. api.tasks.s2
[2023-06-28 11:20:33,940: INFO/MainProcess] Connected to redis://127.0.0.1:6379//
[2023-06-28 11:20:33,946: INFO/MainProcess] mingle: searching for neighbors
[2023-06-28 11:20:34,959: INFO/MainProcess] mingle: all alone
[2023-06-28 11:20:34,965: INFO/MainProcess] celery@QHT81 ready.
[2023-06-28 11:20:38,046: INFO/MainProcess] Received task: api.tasks.s1[7b925061-eab0-4e5a-81fe-2710b5809d64] ETA:[2023-06-28 03:20:48.044882+00:00]
[2023-06-28 11:20:48,056: INFO/MainProcess] Task api.tasks.s1[7b925061-eab0-4e5a-81fe-2710b5809d64] succeeded in 0.0s: 9
[2023-06-28 11:21:02,920: INFO/MainProcess] Received task: api.tasks.s1[e1cbafef-ef65-4ade-b139-d4af31500da5] ETA:[2023-06-28 03:21:12.918404+00:00]
[2023-06-28 11:21:12,930: INFO/MainProcess] Task api.tasks.s1[e1cbafef-ef65-4ade-b139-d4af31500da5] succeeded in 0.0s: 9
到了这里,关于Django+celery开启时报错的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!