gpt4 book ai didi

python - celery 与 Django - MaybeEncodingError : Error sending result

转载 作者:行者123 更新时间:2023-11-30 22:47:44 26 4
gpt4 key购买 nike

我在我的项目中使用下面提到的技能在后台解析 CSV 文件。

Celery 3.0.22

Django 1.6.4

Python 2.7

MySQL 14.14

Distrib 5.5.38

Linux amzn1.x86_64

RabbitMQ 3.1.5

有时我在 celery 日志中收到错误消息“OperationalError: (2006, 'MySQL server has gone away').”和 MaybeEncodingError:发送结果时出错:'""'。原因:''PicklingError("Can\'t pickle : attribute lookup msi.models.DoesNotExist failed",)''。

请在下面找到完整的回溯,

[2015-03-10 12:54:45,344: INFO/MainProcess] Received task: msi.tasks.UploadEventFileBackground[6572cbd7-c410-41d2-b62a-390e011b1896]
[2015-03-10 12:54:45,603: ERROR/MainProcess] Task msi.tasks.UploadEventFileBackground[6572cbd7-c410-41d2-b62a-390e011b1896] raised unexpected: "'. Reason: ''PicklingError("Can\'t pickle : attribute lookup msi.models.DoesNotExist failed",)''.>
Traceback (most recent call last):
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/billiard/pool.py", line 364, in workloop
put((READY, (job, i, result, inqW_fd)))
MaybeEncodingError: Error sending result: '""'. Reason: ''PicklingError("Can\'t pickle : attribute lookup msi.models.DoesNotExist failed",)''.
[2015-03-11 00:47:32,796: INFO/MainProcess] Received task: msi.tasks.UploadDatabaseFileBackground[5f68e619-13cd-425b-998c-1ddf85fdadc2]
[2015-03-11 00:47:33,028: WARNING/Worker-1] /opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/celery/app/trace.py:364: RuntimeWarning: Exception raised outside body: OperationalError(2006, 'MySQL server has gone away'):
Traceback (most recent call last):
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/celery/app/trace.py", line 306, in trace_task
retval=retval, state=state)
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/celery/utils/dispatch/signal.py", line 166, in send
response = receiver(signal=self, sender=sender, *named)
File "/opt/msi/projects/adsoft/msi/tasks.py", line 411, in task_postrun_handler
task_status_instance = CeleryTaskStatus.objects.get(task=task_id)
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/models/manager.py", line 151, in get
return self.get_queryset().get(args, **kwargs)
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/models/query.py", line 301, in get
num = len(clone)
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/models/query.py", line 77, in len
self.fetch_all()
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/models/query.py", line 854, in fetch_all
self._result_cache = list(self.iterator())
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter
for rows in self.execute_sql(MULTI):
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 124, in execute
return self.cursor.execute(query, args)
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/opt/msi/virtualenv/adsoft/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (2006, 'MySQL server has gone away')
exc, exc_info.traceback)))
[2015-03-11 00:47:33,044: CRITICAL/MainProcess] Task msi.tasks.UploadDatabaseFileBackground[5f68e619-13cd-425b-998c-1ddf85fdadc2] INTERNAL ERROR: OperationalError(2006, 'MySQL server has gone away')

最佳答案

显然您遇到了 DoesNotExist 异常,它似乎无法被 pickle(pickle 是用于 celery 的标准序列化方法;例如,您也不能 pickle 请求对象)。

首先,我建议您找出错误发生的位置。

根据经验,无论何时访问模型管理器函数“get”,都应该将其封装在 try-except block 中。

例如:

try:
block = models.Block.objects.get(id=20)
except models.Block.DoesNotExist:
pass # this object does not exist, but you script continues.

关于python - celery 与 Django - MaybeEncodingError : Error sending result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29116515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com