我在我的项目中使用下面提到的技能在后台解析CSV文件。
芹菜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
在某个时候,我在芹菜日志中遇到错误,"OperationalError:(2006,'MySQL服务器已经消失')。"和MaybeEncodingError:发送结果时出错:"。原因:"PicklingError("无法pickle:属性查找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')
显然,您遇到了DoesNotExist异常,该异常似乎无法进行pickle(pickle是用于芹菜的标准序列化方法;例如,您也可以不pickle请求对象)。
首先,我建议你找出错误发生的地方。
根据经验,无论何时访问Model Manger函数"get",都应该将其封装在try-except块中。
例如:
try:
block = models.Block.objects.get(id=20)
except models.Block.DoesNotExist:
pass # this object does not exist, but you script continues.