尝试在celery任务中运行一个简单的命令,但每次尝试执行时都会'获取2个参数(1个给定)'。
我能够毫无问题地执行'添加'芹菜任务,我在芹菜日志中看到它告诉我结果(我正在调试)...但是当我尝试运行bash_run任务时它有例外
[2014-09-24 22:17:08,240: DEBUG/MainProcess] TaskPool: Apply <function _fast_trace_task at 0x10dba12a8> (args:('tasks.bash_run', 'c75860c4-71b2-4ce6-881d-3ed208038891', ('/bin/sleep 50',), {}, {'utc': True, u'is_eager': False, 'chord': None, u'group': None, 'args': ('/bin/sleep 50',), 'retries': 0, u'delivery_info': {u'priority': 0, u'redelivered': None, u'routing_key': u'celery', u'exchange': u'celery'}, 'expires': None, u'hostname': 'celery@MKT2AHF1G3', 'task': 'tasks.bash_run', 'callbacks': None, u'correlation_id': u'c75860c4-71b2-4ce6-881d-3ed208038891', 'errbacks': None, 'timelimit': (None, None), 'taskset': None, 'kwargs': {}, 'eta': None, u'reply_to': u'98d88cb9-6883-3f1d-a1ef-422df9175c88', 'id': 'c75860c4-71b2-4ce6-881d-3ed208038891', u'headers': {}}) kwargs:{})
[2014-09-24 22:17:08,243: DEBUG/MainProcess] Task accepted: tasks.bash_run[c75860c4-71b2-4ce6-881d-3ed208038891] pid:21290
[2014-09-24 22:17:08,246: ERROR/MainProcess] Task tasks.bash_run[c75860c4-71b2-4ce6-881d-3ed208038891] raised unexpected: TypeError('bash_run() takes exactly 2 arguments (1 given)',)
Traceback (most recent call last):
File "/Users/andy.terhune/coregrapherenv/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/Users/andy.terhune/coregrapherenv/lib/python2.7/site-packages/celery/app/trace.py", line 437, in __protected_call__
return self.run(*args, **kwargs)
TypeError: bash_run() takes exactly 2 arguments (1 given)
我的代码:
SECRET_KEY = 'not_a_secret'
CELERY_BROKER_URL='redis://localhost:6379/0'
CELERY_RESULT_BACKEND='redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']
CELERY_ANNOTATIONS = {'tasks.bash_run': {'rate_limit': '2/s'}}
from celery import Celery
import settings
import flask
import os
def make_celery(app):
celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
app = flask.Flask(__name__)
app.config.from_object(settings)
app.wsgi_app = ProxyFix(app.wsgi_app)
celery = make_celery(app)
@celery.task(name="tasks.bash_run")
def bash_run(command):
f = os.popen(command)
output = f.read()
return output
@celery.task(name="tasks.add")
def add(x, y):
return x + y
@app.route('/', methods=['GET'])
def get_stuff():
#working...
#res=add.delay(23,43)
#print res.task_id
#print res.get()
#not working...
string = '/bin/sleep 50'
res = bash_run.delay(string)
print res.task_id
答案 0 :(得分:0)
您可能想尝试将bash_run任务放在自己的模块中。我遇到了同样的错误。当在同一个模块中发生多个不同的方法签名时,猜测Celery会感到困惑,我将失败的任务移到了自己的模块中。问题随我而去。
答案 1 :(得分:-1)
让它工作但不知道为什么这个hacky解决方案是必要的,也许是一个bug?
@celery.task(name="tasks.bash_run")
def bash_run(dummy, command):
f = os.popen(command)
output = f.read()
return output
def get_stuff():
#working...
#res=add.delay(23,43)
#print res.task_id
#print res.get()
#not working...
string = '/bin/sleep 50'
res = bash_run.delay('', string)
print res.task_id
答案 2 :(得分:-2)
很奇怪,我刚才遇到同样的问题,试图执行这项任务:
@app.task(bind=True)
def task_signup_email(user_id):
# fetch user
user = User.objects.get(id=user_id)
# send verification email
send_verification_email(user)
因为它位于tasks.py
文件中,并且芹菜被配置为在Django应用程序中自动发现任务&#39; tasks.py
文件bind=True
是多余的。一旦我删除bind=True
参数,一切都恢复正常,这意味着它不再需要两个参数: - \