Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: RunningCommand.wait(_timeout) #515

Closed
MatrixManAtYrService opened this issue Mar 7, 2020 · 3 comments
Closed

Feature Request: RunningCommand.wait(_timeout) #515

MatrixManAtYrService opened this issue Mar 7, 2020 · 3 comments
Assignees
Labels

Comments

@MatrixManAtYrService
Copy link

Firstly, thanks for your work on sh, it's great.

I would like to be able to do this:

running_command.terminate()
try:
    running_command.wait(_timeout=15) 
except sh.TimeoutException:
    running_command.kill()
running_command.wait()

But the wait function doesn't accept a timeout. Is there a convenient way to achieve this elsewhere in the API? If not, I'd like to propose the above method.

@MatrixManAtYrService
Copy link
Author

MatrixManAtYrService commented Mar 7, 2020

In the meantime, I find that this works reasonably well:

def term_wait_kill(self):
    self.terminate()
    for _ in range(15):
        sleep(1)
        if self.process.is_alive()[0]:
            continue
    if self.process.is_alive()[0]:
        self.kill()
        self.wait()
running_command.term_wait_kill = lambda: term_wait_kill(running_command)

... elsewhere ...

running_command.term_wait_kill()

@amoffat
Copy link
Owner

amoffat commented Mar 7, 2020

Yeah, either that or you could do possibly something fancy (and ugly) with signal.alarm. I like the suggestion though, and agree it should be added.

@amoffat amoffat added the feature label Mar 7, 2020
@amoffat amoffat self-assigned this Apr 26, 2020
@amoffat
Copy link
Owner

amoffat commented Apr 26, 2020

import sh

p = sh.sleep(5, _bg=True)
try:
    p.wait(timeout=1)
except sh.TimeoutException:
    print("killing")
    p.kill()

Will go out with the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants