#๐Ÿ”’ cannot local variable 'pro' where it is not associated with a value

29 messages ยท Page 1 of 1 (latest)

ancient sundial
#

i dont see the issue, it should be in the same scope

def main() -> None:
    try:
        pro = subprocess.call('go run .', timeout=210)
    except subprocess.TimeoutExpired:
        pass

    pro.terminate()
mighty quartzBOT
#

@ancient sundial

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hearty coyote
#

Use finally

crystal valley
#

What happens in the event the exception is triggered?

#

Have you created pro after that point?

ancient sundial
hearty coyote
#

No probs

fluid plinth
#
try:
  x = 1 / 0
except:
  pass
finally:
  print(x) # NameError
try:
  x = 1 / 0
except:
  pass
else:
  print(x) # never runs
vague anchor
#

Indeed, you can only terminate when the call was successful.

#

On the other hand, when the call was successful then it already terminated. lemon_thinking

#

Wait but call doesn't even return a process.

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

Run the command described by args. Wait for command to complete, then return the returncode attribute.
fluid plinth
#

yeah i think you'll need Popen to actually implement the behavior you're looking for

#

(if it doesn't already terminate the process after timing out?)

hearty coyote
#

I was thinking you donโ€™t need to terminate in this case. It automatically happens when you run into an error.

#

Try and except should be enough alone

fluid plinth
#

funny enough the docs don't actually mention what the timeout parameter does.

I assume it's the same behavior as the newer run interface though

#

so subprocess.call isn't even going to stop blocking until the child process is dead anyway

hearty coyote
#

The time it waits before it terminates the process

vague anchor
#

The subprocess.call description does not say what it will do with the timeout parameter. Only that it won't pass it to the Popen constructor.

fluid plinth
#

doesn't seem to explicitly mention that the process will be "terminated and waited for" in the event of a timeout, though

vague anchor
#

That's true.

mighty quartzBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.