0

I'm trying to use zc.lockfile. I see that a lockfile is created in the same directory as my python script, but when I press ctrl+C, the file is NOT removed. I have a callback registered and have even tested given a long time (not sure if zc.lockfile spawns a new thread and needed time to complete).

import os
import sys
import signal
import time
import zc.lockfile

program_lock = None

def onExitCodePressed(signal, frame):
  """Callback run on a premature user exit."""
  global program_lock
  print '\r\nYou pressed Ctrl+C'
  program_lock.close()
  time.sleep(5)
  sys.exit(0)

def main():
  signal.signal(signal.SIGINT, onExitCodePressed)

  if os.path.exists('myapp_lock'):
    print "\nAnother instance of the program is already running.\n"
    sys.exit(0)
  else:
    program_lock = zc.lockfile.LockFile('myapp_lock')

  while True:
    continue


if __name__ == '__main__':
  main()
tarabyte
  • 17,837
  • 15
  • 76
  • 117
  • 2
    Dumb question, but are you actually calling main()? Because if this is your exact code, nothing will run. – beiller Jul 14 '14 at 19:35
  • yes i am, i was just trying to keep the code simple, editing now... – tarabyte Jul 14 '14 at 20:14
  • I really think this really overkill for just creating a file and then checking for it's existence. That library doesn't seem to work (I can't ever properly close or remove the file). – tarabyte Jul 15 '14 at 21:12

0 Answers0