在芹菜文档中,实例化(http://celery.readthedocs.org/en/latest/userguide/tasks.html#custom-task-classes)部分说明如下:
没有为每个请求实例化任务,但是在任务注册表中将任务注册为全局实例。
这意味着每个进程只会调用一次init构造函数,并且任务类在语义上更接近于Actor。
然而,当我运行以下示例时,我看到 init 方法被调用至少3次。设置有什么问题? CELERYD_CONCURRENCY = 1
应该确保每个工作只有一个进程,对吧?
$ celery -A proj beat
celery beat v3.1.17 (Cipater) is starting.
init Task1
40878160
x=1.0
init Task1
40878352
x=1.0
init Task1
40879312
x=1.0
__ - ... __ - _
Configuration ->
. broker -> amqp://guest:**@localhost:5672//
. loader -> celery.loaders.app.AppLoader
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%INFO
. maxinterval -> now (0s)
[2015-02-05 23:05:21,875: INFO/MainProcess] beat: Starting...
[2015-02-05 23:05:21,971: INFO/MainProcess] Scheduler: Sending due task task1-every-5-seconds (proj.tasks.t1)
[2015-02-05 23:05:26,972: INFO/MainProcess] Scheduler: Sending due task task1-every-5-seconds (proj.tasks.t1)
celery.py:
from __future__ import absolute_import
from datetime import timedelta
from celery import Celery
app = Celery('proj',
broker='amqp://guest@localhost//',
backend='amqp://',
include=['proj.tasks'])
app.conf.update(
CELERY_REDIRECT_STDOUTS=True,
CELERY_TASK_RESULT_EXPIRES=60,
CELERYD_CONCURRENCY = 1,
CELERYBEAT_SCHEDULE = {
'task1-every-5-seconds': {
'task': 'proj.tasks.t1',
'schedule': timedelta(seconds=5)
},
},
CELERY_TIMEZONE = 'GMT',
)
if __name__ == '__main__':
app.start()
tasks.py:
from __future__ import absolute_import
from proj.celery import app
from celery import Task
import time
class Foo():
def __init__(self, x):
self.x = x
class Task1(Task):
abstract = True
def __init__(self):
print "init Task1"
print id(self)
self.f = Foo(1.0)
print "x=1.0"
@app.task(base=Task1)
def t1():
t1.f.x +=1
print t1.f.x
答案 0 :(得分:0)
因此,根据您的评论,您需要为每个线程维护一个连接。
为什么不使用线程存储呢?在您的情况下, 应该是一个安全的解决方案。
from threading import local
thread_storage = local()
def get_or_create_conntection(*args, **kwargs):
if not hasattr(thread_storage, 'connection'):
thread_storage.connection = Connection(*args, **kwargs)
return thread_storage.connection
@app.task()
def do_stuff():
connection = get_or_create_connection('some', connection='args')
connection.ping()