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
  Need a script to replace an existing Python installation in a version-independent dir pstein 7 145 May-09-2026, 01:04 PM
Last Post: DeaD_EyE
  Merge Script Functionality in a Single Script gksharma 8 183 Feb-04-2026, 11:47 PM
Last Post: deanhystad
  Py script to APP on MAC tester_V 3 774 Dec-05-2025, 08:12 PM
Last Post: tester_V
  Trying to install the edk2-rk3399 code but the script fails due some python bug mariozio 2 1,918 Aug-17-2025, 10:25 PM
Last Post: mariozio
  Running script from remote to server invisiblemind 4 1,836 Mar-28-2025, 07:57 AM
Last Post: buran
  Insert command line in script lif 4 2,200 Mar-24-2025, 10:30 PM
Last Post: lif
  modifying a script mackconsult 1 1,128 Mar-17-2025, 04:13 PM
Last Post: snippsat
  help with a script that adds docstrings and type hints to other scripts rickbunk 1 1,894 Feb-24-2025, 05:12 AM
Last Post: from1991
  pass arguments from bat file to pyhon script from application absolut 2 2,880 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  Best way to feed python script of a file absolut 6 2,504 Jan-11-2025, 07:03 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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