Django3+celery4.4报错DatabaseWrapper objects created in a thread can only be used in that same thread.
执行异步任务时报错:
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 2068603070080 and this is thread id 2069065236128.
解决方法:
原先启动命令:
windows: celery -A xxx worker -l info -P eventlet 可能需安装 eventletLinux: celery -A xxx worker -l info
替换为:
windows: celery -A xxx worker -l info -P eventlet --pool=soloLinux: celery -A xxx worker -l info --pool=solo
File"/home/celery/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py",line65,incheck_sqlite_versionraiseImproperlyConfigured('SQLite 3.8.3 or later is required (found %s).'%Database.sqlite_version)django.core.exceptions.ImproperlyConfigured:SQLite3.8.3orlaterisrequired (found 3.7.17).
(application) [root@localhost application]# pythonPython 3.6.5 (default, Jul 92019,20:03:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linuxType "help","copyright","credits"or"license"for more information.>>>import sqlite3>>> sqlite3.sqlite_version'3.30.1'>>>exit()
Django3+celery4.4报错DatabaseWrapper objects created in a thread can only be used in that same thread.
执行异步任务时报错:
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 2068603070080 and this is thread id 2069065236128.
解决方法:
原先启动命令:
windows: celery -A xxx worker -l info -P eventlet 可能需安装 eventletLinux: celery -A xxx worker -l info
替换为:
windows: celery -A xxx worker -l info -P eventlet --pool=soloLinux: celery -A xxx worker -l info --pool=solo
celery -A celery_tasks worker -l info --pool=solo -P eventlet
linux启动命令:
celery -A celery_tasks worker -l info
执行CELERY任务
启动完毕celery之后,那么下面进行交互模式进行测试,执行如下:
In [1]: from celery_tasks.tasks import send_register_active_email
In [2]: to_email = 'lijw@******.cn'
In [3]: token = '123456'
In [4]: username = 'lijw'
In [5]: send_register_active_email.delay(to_email,username,token)
Out[5]: <AsyncResult: 02deebc0-5d64-43f9-9ad5-5ccfe260ec70>
12345678910
执行完毕任务后,查看celery的执行日志,如下:
[2019-10-21 14:01:09,508: INFO/MainProcess] celery@junwei1 ready.
[2019-10-21 14:01:09,509: INFO/MainProcess] pidbox: Connected to redis://127.0.0.1:6379/8.
[2019-10-21 14:01:14,353: INFO/MainProcess] Received task: celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a]
# 任务第一次执行,然后执行失败
[2019-10-21 14:01:14,354: WARNING/MainProcess] =========== 执行发送邮件 ===============
[2019-10-21 14:01:15,311: INFO/MainProcess] Received task: celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] ETA:[2019-10-21 06:01:18.21
8857+00:00]
[2019-10-21 14:01:15,345: INFO/MainProcess] Task celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] retry: Retry in 3s: gaierror(11001, 'N
o address found')
# 当执行错误之后,下面则会重试执行5次任务,直到成功,或者失败
[2019-10-21 14:01:18,224: WARNING/MainProcess] =========== 执行发送邮件 ===============
[2019-10-21 14:01:18,253: INFO/MainProcess] Received task: celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] ETA:[2019-10-21 06:01:21.22
5853+00:00]
[2019-10-21 14:01:18,265: INFO/MainProcess] Task celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] retry: Retry in 3s: gaierror(11001, 'N
o address found')
[2019-10-21 14:01:21,227: WARNING/MainProcess] =========== 执行发送邮件 ===============
[2019-10-21 14:01:21,255: INFO/MainProcess] Received task: celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] ETA:[2019-10-21 06:01:24.22
8790+00:00]
[2019-10-21 14:01:21,264: INFO/MainProcess] Task celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] retry: Retry in 3s: gaierror(11001, 'N
o address found')
[2019-10-21 14:01:24,243: WARNING/MainProcess] =========== 执行发送邮件 ===============
[2019-10-21 14:01:24,280: INFO/MainProcess] Received task: celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] ETA:[2019-10-21 06:01:27.24
4729+00:00]
[2019-10-21 14:01:24,291: INFO/MainProcess] Task celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] retry: Retry in 3s: gaierror(11001, 'N
o address found')
[2019-10-21 14:01:27,245: WARNING/MainProcess] =========== 执行发送邮件 ===============
[2019-10-21 14:01:27,271: INFO/MainProcess] Received task: celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] ETA:[2019-10-21 06:01:30.24
6720+00:00]
[2019-10-21 14:01:27,281: INFO/MainProcess] Task celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] retry: Retry in 3s: gaierror(11001, 'N
o address found')
[2019-10-21 14:01:30,261: WARNING/MainProcess] =========== 执行发送邮件 ===============
[2019-10-21 14:01:30,279: ERROR/MainProcess] Task celery_tasks.tasks.send_register_active_email[7b921776-f6a6-4da0-9c71-b04aac0d139a] raised unexpected: gaierror(11001, 'N
o address found')
Traceback (most recent call last):
....
raise socket.gaierror(socket.EAI_NONAME, 'No address found')
socket.gaierror: [Errno 11001] No address found
123456789101112131415161718192021222324252627282930313233343536373839