修复 Celery 问题'can't pickle <class 'module'>: builtins 上的属性查找模块失败

Fix Celery Issue 'can't pickle <class 'module'>: attribute lookup module on builtins failed

我在 运行ning celery 4.1.1 on windows 并向 redis 发送请求(在 ubuntu),Redis 已正确连接并从 windows 端进行测试.但是当我运行这个命令

celery -A acmetelbi worker --loglevel=info

我收到这么长的错误:

[tasks]
. accounts.tasks.myprinting
. acmetelbi.celery.debug_task
 [2019-08-02 11:46:44,515: CRITICAL/MainProcess] Unrecoverable error: 
PicklingErr
or("Can't pickle <class 'module'>: attribute lookup module on builtins 
failed",)

Traceback (most recent call last):
File "c:\acmedata\virtualenv\bi\lib\site- 
packages\celery\worker\worker.py", line 205, in start
self.blueprint.start(self)
File "c:\acmedata\virtualenv\bi\lib\site-packages\celery\bootsteps.py", 
line 119, in start step.start(parent)
File "c:\acmedata\virtualenv\bi\lib\site-packages\celery\bootsteps.py", 
line 370, in start return self.obj.start()
File "c:\acmedata\virtualenv\bi\lib\site- 
packages\celery\concurrency\base.py",
line 131, in start self.on_start()
File "c:\acmedata\virtualenv\bi\lib\site- 
packages\celery\concurrency\prefork.p
y", line 112, in on_start
**self.options)
File "c:\acmedata\virtualenv\bi\lib\site-packages\billiard\pool.py", line 
1007 , in __init__ self._create_worker_process(i)
File "c:\acmedata\virtualenv\bi\lib\site-packages\billiard\pool.py", line 
1116, in _create_worker_process w.start()
File "c:\acmedata\virtualenv\bi\lib\site-packages\billiard\process.py", 
line 124, in start self._popen = self._Popen(self)
File "c:\acmedata\virtualenv\bi\lib\site-packages\billiard\context.py", 
line 383, in _Popen return Popen(process_obj)
File "c:\acmedata\virtualenv\bi\lib\site- 
packages\billiard\popen_spawn_win32.py", 
line 79, in __init__ reduction.dump(process_obj, to_child)
File "c:\acmedata\virtualenv\bi\lib\site-packages\billiard\reduction.py", 
line 99, in dump ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <class 'module'>: attribute lookup 
module on builtins failed

(bi) C:\acmedata\bi_solution\acmetelbi>Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\acmedata\virtualenv\bi\lib\site-packages\billiard\spawn.py", 
line 165, in spawn_main exitcode = _main(fd)
File "c:\acmedata\virtualenv\bi\lib\site-packages\billiard\spawn.py", 
line 207, in _main self = pickle.load(from_parent)
EOFError: Ran out of input

我摸不着头脑,无法理解如何解决这个问题。请帮忙!

我在 Django 应用程序中创建任务的代码。

@task()
def myprinting(self):
    print("I am task")

并在 settings.py 中:

其他芹菜设置

CELERY_BEAT_SCHEDULE = {
'task-number-one': {
    'task': 'accounts.tasks.myprinting',
    'schedule': crontab(minute='*/30'),
     },

经过多天的研究,我得出结论,芹菜对 windows 有限制,如果你想 运行 对 windows 芹菜,那么你必须 运行 它与 gevent 命令:

python manage.py celery worker -P gevent --loglevel=INFO

然后在 运行ning 这个工作进程之后相应地启动 celery beat 以开始处理。