如何使芹菜与gevent一起工作?

时间:2016-08-01 07:30:52

标签: python celery gevent

关注the example,芹菜以celery worker -P gevent -A app.celery --loglevel=INFO

开头

启动标题显示:

- ** ---------- .> app:         app:0x10fab2a10
- ** ---------- .> transport:   redis://localhost:6379/0
- ** ---------- .> results:     redis://localhost:6379/0
- *** --- * --- .> concurrency: 4 (gevent)

请注意,并发性设置为4

app.py中的

import gevent
@celery.task(bind=True)
def add_five_times(self, x, y):
    for i in range(4):
        z = x + y
        print self.request.id
        gevent.sleep(5)
    return x + y

在与app.py相同的目录中,我启动了ipython并启动了5个任务

from app import add_five_times
tasks = [add_five_times.delay(i, i*2) for i in range(5)]

在芹菜终端中,日志消息是(GUID已更改以便于准备):

Received task: app.add_five_times[id-1]
Received task: app.add_five_times[id-2]
Received task: app.add_five_times[id-3]
Received task: app.add_five_times[id-4]
Received task: app.add_five_times[id-5]
id-1  // from print statement in add_five_times
id-2
id-3
id-4
....
repeatedly logging id-1 to id-4
Task app.add_five_times[id-1] succeeded in 20.021114759s: 0
Task app.add_five_times[id-2] succeeded in 20.015921234s: 3
Task app.add_five_times[id-3] succeeded in 20.020661308s: 6
Task app.add_five_times[id-3] succeeded in 20.016252998s: 6
id-5 // from print statement in add_five_times
id-5
id-5
id-5
Task app.add_five_times[id-5] succeeded in 20.021114759s: 12

我期望任务在gevent.sleep之间交错。 我错过了什么?需要做出哪些改变?

  • gevent 1.1.2
  • 芹菜3.1.23
  • python 2.7.11

0 个答案:

没有答案