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

Popen.poll() doesn't reflect the returncode value when using a callback #164

Open
inorton opened this issue Aug 1, 2024 · 3 comments
Open

Comments

@inorton
Copy link

inorton commented Aug 1, 2024

I have a number of callers that do..

proc = subprocess.Popen(cmdline)
while proc.poll() is None:
    time.sleep(1)

I register with:

def my_callback():
    print("called")
    time.sleep(1)

fp.register(cmdline, returncode=1, callback=my_callback)

But proc.poll() seems to always return None

@inorton inorton changed the title Popen.poll() doesn't reflect the returncode value when set Popen.poll() doesn't reflect the returncode value when using a callback Aug 1, 2024
@aklajnert
Copy link
Owner

The returncode is ignored when passing callback, as the callback was meant to modify the returncode like in the example below:

def test_poll(fp):
    def my_callback(proc):
        print("called")
        time.sleep(1)
        proc.returncode = 1

    fp.register("test", returncode=1, callback=my_callback)

    proc = subprocess.Popen("test")
    while proc.poll() is None:
        time.sleep(1)

This is a bit counterintuitive, but I'm not sure how it is supposed to be fixed. I see two options:

  • just raise an exception when callback and returncode are provided together, and inform users that the returncode shall be set within a callback,
  • set the returncode if it was never modified by a callback

The second option seems to be better, but it could confuse people that are changing the returncode within the callback, but still expect it to finally land on the original returncode provided in the fp.register().

@inorton
Copy link
Author

inorton commented Aug 13, 2024

Oh I see, I must have missed that. It would be good perhaps to provide a type hint for the callback?

@aklajnert
Copy link
Owner

I'll think about it. It may be confusing also for other, so some warning or even error in such cases could be beneficial. I'll keep this issue open until I'll figure something out.

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

No branches or pull requests

2 participants