芹菜与Django - MaybeEncodingError:发送结果时出错

时间:2015-03-18 07:41:50

标签: python mysql django rabbitmq celery

我在我的项目中使用下面提到的技能来解析后台的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

在某些时候我在芹菜日志中遇到错误"操作错误:(2006年,' MySQL服务器已经消失了')。"和MaybeEncodingError:发送结果时出错:'""'。原因:'' PicklingError("无法挑选:属性查找msi.models.DoesNotExist失败",)''。

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

[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')

1 个答案:

答案 0 :(得分:0)

显然你遇到了一个看似无法被腌制的DoNotExist异常(pickle是用于芹菜的标准序列化方法;例如,你也不能挑选一个请求对象。)

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

根据经验,无论何时访问Model Manger函数“get”,都应将其封装在try-except块中。

E.g:

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