Last active
July 5, 2018 19:18
-
-
Save gregjhogan/17bfe8ecd0ffcb7f19607d191ddd3a51 to your computer and use it in GitHub Desktop.
python fork and skip finally blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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