You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInvoke异步进程:读取当前输出及手动终止问题咨询

关于Invoke异步任务Promise对象的输出获取与进程终止问题

背景说明

官方文档提到了asynchronous=True的用法,但缺少完整的示例代码片段。我常需要查阅GitHub Issue找示例,可能是我漏看了文档内容,已提交相关Issue。

测试代码

@task
def my_cmd(ctx: Context):
    invoke_promise = ctx.run(
        "while true; do echo running forevah; sleep 2; done", warn=True, hide=False, echo=True, asynchronous=True
    )

遇到的问题

  1. 执行print(invoke_promise)触发报错:
print(invoke_promise)

Traceback (most recent call last):
  File "/usr/local/bin/myproject", line 8, in <module>
    sys.exit(program.run())
  File "/usr/local/lib/python3.10/site-packages/invoke/program.py", line 384, in run
    self.execute()
  File "/usr/local/lib/python3.10/site-packages/invoke/program.py", line 569, in execute
    executor.execute(*self.tasks)
  File "/usr/local/lib/python3.10/site-packages/invoke/executor.py", line 129, in execute
    result = call.task(*args, **call.kwargs)
  File "/usr/local/lib/python3.10/site-packages/invoke/tasks.py", line 127, in __call__
    result = self.body(*args, **kwargs)
  File "/workspaces/myproject/myproject/tasks/mytask.py", line 479, in my_cmd
    print(invoke_promise)
  File "/usr/local/lib/python3.10/site-packages/invoke/runners.py", line 1475, in __str__
    if self.exited is not None:
AttributeError: 'Promise' object has no attribute 'exited'
  1. 执行print(invoke_promise.stdout)同样报错:
print(invoke_promise.stdout)
AttributeError: 'Promise' object has no attribute 'stdout'
  1. 执行print(invoke_promise.join())会一直挂起直到进程结束。

需求

  • 无需等待进程结束,查看当前已产生的输出
  • 读取运行中进程的输出,并可手动终止该进程

内容的提问来源于stack exchange,提问作者red888

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 11:12:40