我正在从 Python 执行 FORTRAN exe。FORTRAN exe 需要几分钟才能完成;因此,我需要在 exe 完成时触发回调。exe 不会将任何内容返回给 Python,但在回调函数中,我将使用 Python 解析来自 FORTRAN 的输出文本文件。
为此,我正在使用concurrent.futures
and add_done_callback()
,它可以工作。但是 Web 服务的这一部分,我需要在执行 FORTRAN exe 后调用subprocess.call() / Popen()
返回的 Python 方法。然后当 FORTRAN 完成时,回调函数被调用。
def fortran_callback(run_type, jid):
return "Fortran finished executing"
def fortran_execute():
from concurrent.futures import ThreadPoolExecutor as Pool
args = "fortran.exe arg1 arg2"
pool = Pool(max_workers=1)
future = pool.submit(subprocess.call, args, shell=1)
future.add_done_callback(fortran_callback(run_type, jid))
pool.shutdown(wait=False)
return "Fortran executed"
提交表单时调用 fortran_execute() 并且我想返回“Fortran 执行”而不等待 FORTRAN 完成。
目前 Python 方法无需等待 FORTRAN 完成即可返回,但它也会在返回时触发回调。FORTRAN 进程继续运行,当它最终完成时,它会尝试调用回调函数并抛出异常,因为future
不再存在TypeError: 'NoneType' object is not callable
。
我在这里缺少什么来启动 exe subprocess
,让函数返回,然后只有在 exe 完成执行时才调用回调方法?