Django中使用celery报错ValueError: I/O operation on closed file

最近在Django中使用celery做费时任务的时候,启动celery时总是会报错ValueError: I/O operation on closed file.在谷歌、百度上搜索后也没有找到相应的解决方案。具体的报错信息如下:

1
2
3
4
5
6
7
8
9
[2017-06-14 11:55:59,173: ERROR/MainProcess] Unrecoverable error: ValueError('I/O operation on closed file.',)
Traceback (most recent call last):
File "c:\users\qiuqingyu\appdata\local\programs\python\python35\lib\site-packages\celery\worker\__init__.py", line 206, in start
self.blueprint.start(self)
File "c:\users\qiuqingyu\appdata\local\programs\python\python35\lib\site-packages\celery\bootsteps.py", line 119, in start
self.on_start()
File "c:\users\qiuqingyu\appdata\local\programs\python\python35\lib\site-packages\celery\apps\worker.py", line 171, in on_start
])), file=sys.__stdout__)
ValueError: I/O operation on closed file.

经过排查后发现在celery的tasks任务中有如下一条语句从而导致celery任务无法运行。

1
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')

将这句话删除之后程序就可以正常运行了。

本文作者:Qiu Qingyu
版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 CN许可协议。转载请注明出处!
本文永久链接:http://qiuqingyu.cn/2017/06/14/Django中使用celery报错ValueError-I-O-operation-on-closed-file/