2015-10-20 61 views
1

我有一個可能排隊其他子任務的芹菜任務。如果工作人員從高優先級隊列中抽取該任務,然後該任務將其他任務排隊,我希望新任務將返回置於高優先級隊列中。但是,我怎樣才能以編程方式獲得當前正在執行的任務來自的隊列呢?如何確保Celery任務的子任務與父任務進入同一隊列?

我知道我可以做一些事情,比如傳遞一個額外的參數給原始的my_task.apply_async()調用,它指定一個用於子任務的隊列,然後我可以通過一系列方法/類來傳遞它,任務,但似乎凌亂,難以維護。看起來隊列信息可以通過詢問Celery來獲得。

回答

1

我發現隊列信息可以通過current_task.request.delivery_info['exchange']獲得。
因此,解決方案,我結束了使用如下:

def get_source_queue(default=None): 
    """ 
    Finds and returns the queue that the currently-executing task (if any) came from. 
    """ 
    from celery import current_task 
    if current_task is not None and 'exchange' in current_task.request.delivery_info: 
     source_queue = current_task.request.delivery_info['exchange'] 
     if source_queue is not None: 
      return source_queue 
    return default 

,然後我使用與子任務是這樣的:

my_task.apply_async(args=('my', 'args'), queue=get_source_queue(default='foo_queue')) 


我不知道如果是這樣的最好的辦法做到這一點...也許有一些東西內置於芹菜,說「使用相同的隊列作爲源隊列」(?)但是,上述作品。

+0

也相關:http://stackoverflow.com/questions/34123455/do-subtasks-inherit-the-queue-of-their-parent-task – Hudon