Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
launching executables
#1
Hi,
I have an executable. Users click on it (or an icon) and do research.
One user however starts inadvertently many instances of the exe,
and one particular feature can cause a crash, if used.
Google proposes some solutions, question is, what is the most recommended?
eg. :
Use a "lock" file: but I can imagine that if the computer
crashes for another reason, or is switched off, the lock file is still there, causing trouble?
Use something like tendo.singleton...?
...?
thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#2
Your two solutions are the same. If you read the documentation for the tendo singleton it uses a lock file.

Most lock file implementations don't use the presence of the file, they use the state of the file. Having the PC, or the program for that matter, crash is not a problem because the file state is forgotten or repaired. A bigger issue is that Linux and Windows files act differently, and you'll need different code for each.
Reply
#3
(Jan-13-2023, 09:20 PM)deanhystad Wrote: Your two solutions are the same. If you read the documentation for the tendo singleton it uses a lock file.
Yes, I understood that much going to the sparse docs. Bit disappointed.
As it is a rare occurence, i will go for a lockfile, that I can put where it
is easy to find in case of problems.
thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#4
(Jan-13-2023, 07:31 AM)DPaul Wrote: Use a "lock" file: but I can imagine that if the computer
crashes for another reason, or is switched off, the lock file is still there, causing trouble?
The lock file should not be empty, but contain te process id of the running process. (Use os.getpid() to obtain the PID of the current process.) Then at the start of your program, when a lockfile exists, check for the existence of a process with the PID mentioned in the lockfile. (Use psutil.pids() to get a list of running processes.) If the process appears not to exist anymore you can safely delete the lockfile and create a new one.
The place for lockfiles in Unix/Linux is: /var/lock/.
Gribouillis likes this post
Reply
#5
From what I read elsewhere the correct place is now /run/lock , although your /var/lock is probably a symlink to /run/lock. It seems that lock files stored there should not persist upon reboots.
ibreeden likes this post
Reply
#6
(Jan-14-2023, 10:33 AM)ibreeden Wrote: The lock file should not be empty, but contain te process id of the running process.
Yes, I also read about how to do that.
For the moment I have an empty lockfile, working like a charm.
I could put the PID in there, no trouble. But I still like to put it
where I want. Smile
Thanks for the good advice!
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#7
Example (Linux only) to check a lock.pid.

import atexit
import os
import time
from pathlib import Path


def run_check():
    """
    Read the lock.pid (next to __file__) if exists
    and check if a process with the pid in the lock.pid is running.
    If the pid is still alive, a SystemExit is thrown.
    If the pid is not alive, the lock.pid is written with the current pid.
    Then the deletion of lock.pid is registered.
    """
    pid_file = Path(__file__).parent.joinpath("lock.pid")
    pid = os.getpid()

    if pid_file.exists():
        last_pid = int(pid_file.read_text())

        try:
            os.kill(last_pid, 0)
        except ProcessLookupError:
            pass
        else:
            raise SystemExit(f"Another process with pid {last_pid} is running.")

    pid_file.write_text(str(pid))
    atexit.register(pid_file.unlink)


if __name__ == "__main__":
    run_check()
    while True:
        time.sleep(1)
But you can also choose a 3rd party module: https://pypi.org/project/pid/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python2.7 executables thus the system python2.7 was erroring utility.execute()? vivekm 1 1,727 May-20-2019, 11:24 AM
Last Post: vivekm
  Launching other programs from Python Truman 4 2,891 Feb-14-2019, 12:59 AM
Last Post: Truman
  Launching executable over rest server api InsatiableBuns 0 1,962 Oct-25-2018, 12:22 PM
Last Post: InsatiableBuns
  Program-launching script will not close until the program it launches is closed mmwatson 1 3,217 Jun-21-2017, 10:31 PM
Last Post: Ofnuts
  Issue in launching spyder that was installed using pip install on Python 3.6.1 python300 6 10,994 Jun-02-2017, 10:44 PM
Last Post: python300

Forum Jump:

User Panel Messages

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