简体   繁体   English

使用Django将列表传递给芹菜工作者

[英]Pass a list to a celery worker with Django

I used Django 1.9 / Python 2.7 / Celery 3.1.23 / Redis 2.10.5 我使用了Django 1.9 / Python 2.7 / Celery 3.1.23 / Redis 2.10.5

Celery works fine for many simple tasks, but when I am trying to pass lists to my Celery worker, it does not work. Celery可以很好地完成许多简单的任务,但是当我尝试将列表传递给我的Celery工作人员时,它不起作用。 The general objective is to lighten the worker process by succesively transmitting chunks of ids to the worker, and not 30,000 all in one time. 总的目的是通过将ID块成功地传输给工人来减轻工人的工作流程,而不是一次发送30,000个。

I understand I have to pass my list as Json format. 我知道我必须将列表作为Json格式传递。

Settings: 设定:

CELERY_ACCEPT_CONTENT = ['json']
CELERY_RESULT_SERIALIZER = ['json']
CELERY_TASK_SERIALIZER = ['json']

tasks.py: task.py:

@periodic_task(run_every=crontab(minute=29, hour=12))
    def update(request=None):
        iddict = [23, 49, 81, 23]
        forceevaluation = list(sliceids)        
        sliceids2 = json.dumps(forceevaluation)    
        updatewikipediadescription2.apply_async(args=sliceids2, eta=now() + timedelta(seconds=a))

views.py views.py

@shared_task
def updatetask(slice):
    for placeid in slice:
        print("ok")

Celery always shows the following error: 芹菜总是显示以下错误:

[2017-03-10 12:29:04,031: ERROR/MainProcess] Task googleautocomplete.tasks.updatetask[94cdded2-0b2d-4304-aa14-6d97257c947c] raised unexpected: ValueError('task args must be a list or tuple',) 

Any idea why I get this error ? 知道为什么我会收到此错误吗?

You're not passing a list, you're passing the ouput of JSON dumping your list. 您没有传递列表,而是传递了JSON转储列表的输出。

sliceids2 = json.dumps(forceevaluation) creates a string: sliceids2 = json.dumps(forceevaluation)创建一个字符串:

>> type(sliceids2) <type 'str'> so when you run: >> type(sliceids2) <type 'str'>因此在运行时:

updatewikipediadescription2.apply_async(args=sliceids2, eta=now() + timedelta(seconds=a))

you're just passing a string. 您只是传递一个字符串。

try: 尝试:

updatewikipediadescription2.apply_async(args=[sliceids2,], eta=now() + timedelta(seconds=a))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM