1

我想在 python 中使用 celery 来使用并发。我有一个 tasks.py 文件,它是一个使用 BeautifulSoup 的网络爬虫。我所做的进口是:

from celery import Celery
import eventlet
app = Celery('tasks', backend='amqp', broker='amqp://')

对于芹菜工人,我使用这个命令:

celery -A tasks worker --loglevel=info --pool=eventlet -c 1000

错误:

mayank@mayank-Studio-1558:~/cognite/test$ celery -A tasks worker --loglevel=info --         pool=eventlet -c 1000
Traceback (most recent call last):
File "/usr/local/bin/celery", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/celery/__main__.py", line 28, in main
maybe_patch_concurrency()
File "/usr/local/lib/python2.7/dist-packages/celery/__init__.py", line 124, in maybe_patch_concurrency
patcher()
File "/usr/local/lib/python2.7/dist-packages/celery/__init__.py", line 89, in _patch_eventlet
  eventlet.monkey_patch()
AttributeError: 'module' object has no attribute 'monkey_patch'

我想使用 1000 个线程执行任务。我试过导入eventlet,然后做了:

eventlet.monkey_patch(socket=True, select=True)
eventlet.import_patched('monkey_patch')

仍然没有工作,同样的错误。

请如果有人可以提供帮助,那就太好了。谢谢。

4

3 回答 3

2

由于 eventlet 安装而出现此错误。以前我使用 pip install eventlet,但sudo apt-get install eventlet对我有用,它解决了monkey_patch依赖项。

于 2014-08-06T08:21:51.130 回答
1

我刚刚遇到了这个问题,对我来说,问题是我的系统中没有 python 开发头文件。当我试图通过 pip 安装 eventlet 时,我收到了这个错误:

In file included from greenlet.c:5:0:

greenlet.h:8:20: fatal error: Python.h: No such file or directory

 #include <Python.h>

                    ^

compilation terminated.

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

但它说无论如何都安装了eventlet,除了我在尝试使用它时遇到了同样的错误。所以在我的情况下如何解决它是:

pip uninstall eventlet
apt-get install python-dev (For Ubuntu, use your distros package manager)
pip install eventlet

希望能帮助到你。

于 2014-09-20T18:44:55.637 回答
0

最可能的原因是您拥有eventlet.py或在您的项目树中的eventlet.pyc某处sys.path,也许在您的项目树中。

试试这个:

from __future__ import absolute_import
from celery import Celery
import eventlet
app = Celery('tasks', backend='amqp', broker='amqp://')
于 2014-08-05T07:36:54.870 回答