Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python芹菜-如何在其他任务中调用芹菜任务_Python_Django_Celery_Djcelery - Fatal编程技术网

Python芹菜-如何在其他任务中调用芹菜任务

Python芹菜-如何在其他任务中调用芹菜任务,python,django,celery,djcelery,Python,Django,Celery,Djcelery,我在Django芹菜的任务中调用任务 这是我的任务 @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

我在Django芹菜的任务中调用任务

这是我的任务

@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
如何调用任务中的任务? 我在某个地方读到,它可以使用
来完成,但我无法形成正确的语法。我该怎么做

我试过这个

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

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 '
不知道是什么


如何解决问题?

您可以使用延迟函数从任务调用任务

from app.tasks import celery_add_task
    celery_add_task.apply_async(args=[task_name]) 
。。。它将起作用

这应该起作用:

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

您是对的,因为您的
for
循环中的每个任务都将被覆盖
task
变量

你可以试试芹菜。分组

from celery import group


什么是my_模型和当前_应用程序?
当前_应用程序
是芹菜模块的属性
mymodel.tasks
是指向
tasks.py
的路径。如有必要,请更改它。由于要调用的任务位于同一模块中,我的操作如下
task=芹菜。当前应用程序。发送任务('post\u notification',args=[data,url])打印任务。状态
请注意,您不必使用
发送任务
,您可以使用
任务。任务延迟
,不会出现问题,您的问题是轮询返回的结果对象。在这里工作正常!谢谢我使用芹菜中的
。\u state将当前应用程序导入到4.2.x版
result=task。延迟
/
task。apply\u async
提供一个
AsyncResult
对象。这支持轮询
.status
属性,每次访问该属性时都会检查任务的状态。在发送任务后立即调用.state是没有意义的,因为工作人员可能还没有开始执行它。在后面的示例中,您调用了
task=..get().status
,这将不起作用,因为您调用的是任务的返回值,而不是结果(result.status vs result.get().status)。最后,您不应该等待子任务的结果,因为这可能导致死锁,而应该使用回调任务:
(post_notification.s()|在post.s()之后做一些事情。).delay()
。请参阅和
@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)


    tasks = [post_notification.s(data, server.server_id.url) for server in server_list]
    results = group(tasks)()
    print results.get() # results.status() what ever you want