Skip to content

Instantly share code, notes, and snippets.

@allenyang79
Created March 12, 2019 07:06
Show Gist options
  • Select an option

  • Save allenyang79/ada1c631eac08944156615bae8abfcd8 to your computer and use it in GitHub Desktop.

Select an option

Save allenyang79/ada1c631eac08944156615bae8abfcd8 to your computer and use it in GitHub Desktop.
keep only one process can run at the same time
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