当我试图将一个字符串作为参数传递到我的芹菜文件中时,出现了以下错误:
celery.beat.SchedulingError: Couldn't apply scheduled task add-every-10-seconds: get_data() takes 1 positional argument but 138 were given
然而,我不太明白为什么这意味着——我将参数作为字符串传递,但不知何故,Celery将其转换为char数组或其他什么。
|-- data
|-- tasks.py
|-- nttracker
|-- celery.py
|-- manage.py
nttracker\cherce.py
from __future__ import absolute_import
app = Celery('nttracker', broker='amqp://', backend='rpc://', include=['nttracker.tasks'])
app.autodiscover_tasks()
app.conf.update(
timezone = "Asia/Taipei",
result_backend = 'django-db',
broker_url = 'redis://127.0.0.1:6379',
cache_backend = 'default',
beat_schedule = {
'test': {
'task': 'data.tasks.get_data',
'schedule': 10.0,
'args': "https://gist.githubusercontent.com/crimsonpython24/8a42e1c7226b73d59dcb2447fa961caa/raw/57441c0912cf51695b113745052c4829459dec02/test.json"
},
}
)
if __name__ == '__main__':
app.start()
数据\芹菜.py
from __future__ import absolute_import
import django
django.setup()
import requests
from celery import Celery
from celery.schedules import crontab
app = Celery()
@app.task
def get_data(url):
r = requests.get(url=url)
data = r.json()
print(data)
有人能帮忙吗?非常感谢。
args
应该是一个可迭代的,它将被解压缩,并且每个元素都作为位置参数传递给任务。
只传递一个字符串会导致整个字符串被解包,每个单独的字符都作为参数传递。您应该将字符串包装在元组或列表中
beat_schedule = {
'test': {
'task': 'data.tasks.get_data',
'schedule': 10.0,
'args': ("https://gist.githubusercontent.com/crimsonpython24/8a42e1c7226b73d59dcb2447fa961caa/raw/57441c0912cf51695b113745052c4829459dec02/test.json", )
},
}