Created
March 12, 2019 07:06
-
-
Save allenyang79/ada1c631eac08944156615bae8abfcd8 to your computer and use it in GitHub Desktop.
keep only one process can run at the same time
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 | |
| import atexit | |
| def keep_single_process(): | |
| pid = str(os.getpid()) | |
| pidfile = "/tmp/main_coupang.pid" | |
| if os.path.isfile(pidfile): | |
| raise Exception("%s already exists, exiting" % pidfile) | |
| with open(pidfile, 'w') as fp: | |
| fp.write(pid) | |
| @atexit.register | |
| def on_exit(): | |
| os.unlink(pidfile) | |
| if __name__ == '__main__': | |
| keep_single_process() | |
| print(1 + 2) | |
| print(2 / 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment