Python Forum
Detect if another copy of a script is running from within the script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detect if another copy of a script is running from within the script
#1
I have a situation where an application can launch a python script at any time. It can also launch the script multiple times almost simultaneously. I tried the following code in the script:

#prevent multiple instances
locker=tmpdir+"/spamcop.lock"
if (os.path.exists(locker)):
    exit
lockfile=open(locker,"w");
lockfile.write("lock")
lockfile.close()
This does not seem to work. I suspect that the multiple scripts are launched so close that both fail the 'if' statement. Is there a better way, within the script itself, to prevent multiple copies running? TIA.
Reply
#2
do you mean multiple processes on the same computer?

what if i have a copy of your script on my laptop (computer) and run your script at the same time? does this matter?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#3
You could perhaps use a library such as filelock, something like (untested code)
import filelock
import sys

def main():
    ...

if __name__ == '__main__':
    lock = filelock.FileLock('mylock.lock')
    try:
        with lock.acquire(blocking=False):
            main()
    except filelock.Timeout:
        sys.exit(1)
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
What is tempdir? If it is a temporary directory created using the tempfile module each instance of the script has its own unique directory. Post more of your code to provide context.
Reply
#5
are you assuming your particular OS will refuse to allow a directory to be created when the request names an existing directory? FYI, i once (back when i was in college) used (by remote dial up) an OS on a fairly large computer (considered a mainframe back then) where that is not true.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running script from remote to server invisiblemind 4 677 Mar-28-2025, 07:57 AM
Last Post: buran
  Insert command line in script lif 4 912 Mar-24-2025, 10:30 PM
Last Post: lif
  modifying a script mackconsult 1 535 Mar-17-2025, 04:13 PM
Last Post: snippsat
  help with a script that adds docstrings and type hints to other scripts rickbunk 2 1,218 Feb-24-2025, 05:12 AM
Last Post: from1991
  pass arguments from bat file to pyhon script from application absolut 2 982 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  Best way to feed python script of a file absolut 6 1,079 Jan-11-2025, 07:03 AM
Last Post: Gribouillis
  How to make it so whatever I input into a script gets outputted on a different file spermatozwario 4 1,129 Nov-24-2024, 12:58 PM
Last Post: deanhystad
  [SOLVED] [Linux] Run Python script through cron? Winfried 2 1,196 Oct-19-2024, 06:29 PM
Last Post: Winfried
  Help for Tiktok Script Jasson187512 0 734 Oct-09-2024, 08:42 AM
Last Post: Jasson187512
  Pyinstaller and a custom python script mentat 0 985 Sep-27-2024, 04:42 PM
Last Post: mentat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020