How do I read the current output with asynchronous without waiting for the process to finish?

Open
#951 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
cli

Research direction

Start with Invoke's documentation for asynchronous=True and the returned Promise, then compare the shown print(invoke_promise) and invoke_promise.join() behavior. Provide complete examples for observing output without waiting and manually stopping the process, or document the limitation if that is not supported.

Written by the indexing model from the issue text.

Description

FYI created an SO post too: https://stackoverflow.com/questions/76491927/how-do-i-read-the-current-output-with-asynchronous-without-waiting-for-the-proce/76492139#76492139

Its documented, but are there any complete examples and snippets for how to use asynchronous=True?

I usually have to look through github issues when trying to find full example snippets to stuff, but maybe I'm just missing this in the docs.

Im doing this

@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
    )

This throws an error

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'

This hangs forever until the process exits
print(invoke_promise.join())

What method is there on the returned promise object to just get the current output of the running background process that was started?

I want to be able to:

  1. Just see the current output so far without waiting for it to finish
  2. Be able to read the output of the currently running process and choose to manually kill it if I want
Dominant language
Python
Stars
4.8k
Forks
412
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pyinvoke/invoke

All issues in pyinvoke/invoke

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.