Python芹菜 - 如何调用其他任务中的芹菜任务 [英] Python Celery - How to call celery tasks inside other task

查看:349
本文介绍了Python芹菜 - 如何调用其他任务中的芹菜任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的任务。

  @shared_task 
def post_notification(data,url):
url =http://posttestserver.com/data/?dir=praful#在生产中,删除这一行。
headers = {'content-type':'application / json'}
requests.post(url,data = json.dumps(data),headers = headers)


@shared_task
def shipping_server(data,notification_type):
notification_obj = Notification.objects.get(name = notification_type)
server_list = ServerNotificationMapping.objects.filter(notification_name = notification_obj)

在server_list中的服务器:
task = post_notification.delay(data,server.server_id.url)
print task.status#它打印Nonetype没有属性id

如何在任务中调用任务?
I在某处可以使用完成,但是我无法形成正确的语法。我如何做?



我尝试过

 code> for server in server_list:
task = group(post_notification.s(data,server.server_id.url))()。get()
print task.status

发出警告说

  TxIsolationWarning:轮询结果w $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $¬$¬$¬$¬
ith交易隔离级别可重复读取在同一个交易│
可能会给出过时的结果。确保为每个│
投票迭代提交交易。 │
'具有交易隔离级别的轮询结果'

不知道是什么!



如何解决我的问题?

解决方案

  celery.current_app.send_task('mymodel.tasks.mytask',args = [arg1,arg2,arg3])


I'm calling a task within a tasks in Django-Celery

Here are my tasks.

@shared_task
def post_notification(data,url):
    url = "http://posttestserver.com/data/?dir=praful" # when in production, remove this line.
    headers = {'content-type': 'application/json'}
    requests.post(url, data=json.dumps(data), headers=headers)


@shared_task
def shipment_server(data,notification_type):
    notification_obj = Notification.objects.get(name = notification_type)
    server_list = ServerNotificationMapping.objects.filter(notification_name=notification_obj)

    for server in server_list:
        task = post_notification.delay(data,server.server_id.url)
        print task.status # it prints 'Nonetype' has no attribute id

How can I call a task within a task? I read somewhere it can be done using group, but I'm not able to form the correct syntax. How do I do it?

I tried this

for server in server_list:
    task = group(post_notification.s(data, server.server_id.url))().get()
    print task.status

Throws a warning saying

TxIsolationWarning: Polling results w│                                                                        
ith transaction isolation level repeatable-read within the same transacti│                                                                        
on may give outdated results. Be sure to commit the transaction for each │                                                                        
poll iteration.                                                          │                                                                        
  'Polling results with transaction isolation level '

Dont know what it is!!!

How do I solve my problem?

解决方案

This should work:

celery.current_app.send_task('mymodel.tasks.mytask', args=[arg1, arg2, arg3])

这篇关于Python芹菜 - 如何调用其他任务中的芹菜任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆