Skip to content

Instantly share code, notes, and snippets.

@gregjhogan
Last active July 5, 2018 19:18
Show Gist options
  • Select an option

  • Save gregjhogan/17bfe8ecd0ffcb7f19607d191ddd3a51 to your computer and use it in GitHub Desktop.

Select an option

Save gregjhogan/17bfe8ecd0ffcb7f19607d191ddd3a51 to your computer and use it in GitHub Desktop.
python fork and skip finally blocks
import os
try:
child_pid = os.fork()
if child_pid == 0:
# finally block runs twice
#exit(1)
# finally block runs once on exception
#try:
# raise Exception('bang!')
#except:
# os._exit(1)
# finally block runs once
os._exit(0)
else:
pid, exit_code = os.waitpid(child_pid, 0)
print("exit: {}".format(exit_code))
except Exception as e:
print("{}: except".format(child_pid))
print(e)
finally:
print("{}: finally".format(child_pid))
print("{}: end".format(child_pid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment