芹菜作业完成后的 Django-Execute 函数



我正在使用带有织物芹菜。我的芹菜任务是将文件复制到我的本地计算机,如下所示:

@app.task
def copytolocal():
 get('/home/remote/file.txt','/home/local/') # Copying to local dir

在视图中,当我尝试打开并读取文件时,我得到一个IOError,因为任务尚未完成

taskfile.app.send_task('taskfile.copytolocal',[]) # Sending task to queue
open('/home/local/file.txt') #IOError because task isnt completed yet           

如果我使用类似 asyncresult.ready() 的东西,那么它会在执行时返回False。有没有办法实现回调之类的东西,或者如何实现。

最简单的方法是这样做

while not asyncresult.ready():
    time.sleep(1)
# Now the file is downloaded

如果你想要它更好一点,你可以使用回调,其中link是在常规任务完成时要执行的任务。

download_file.apply_async(path, link=write_to_db.subtask(path))

最新更新