我正在使用 Celery 与 Fabric 。我的芹菜任务是将文件复制到我的本地机器上,如下所示:
@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
。
有没有办法实现像回调或其他方法,以及如何做到这一点。
答案 0 :(得分:1)
简单的方法就是
while not asyncresult.ready():
time.sleep(1)
# Now the file is downloaded
如果你想要它更好一点,你可以使用callbacks link
是在常规任务完成时要执行的任务。
download_file.apply_async(path, link=write_to_db.subtask(path))