Determine what the calling program is

Dan Stromberg drsalists at gmail.com
Sun Apr 18 19:48:02 EDT 2021


I use this little program for shell-level locking.

It just checks for a pid file.  If the pid file does not exist, or the pid
no longer exists, it'll start the process, and write the new process' pid
to the pid file.

It's at:
https://stromberg.dnsalias.org/svn/just-one/

...and usage looks like:
$ ./just-one -h
below cmd output started 2021 Sun Apr 18 04:44:03 PM PDT
Usage: ./just-one --command command --string string

The "string" part needs to be a unique identifier for each process you want
only one of.  The command, naturally, is a shell command.

I just noticed that I don't have a web page describing it yet.  I'll
probably set one up a little later.

It does not use locking: advisory or mandatory.  Just a lock file
containing a pid.

HTH.


On Sun, Apr 18, 2021 at 6:47 AM Jason Friedman <jsf80238 at gmail.com> wrote:

> I should state at the start that I have a solution to my problem. I am
> writing to see if there is a better solution.
>
> I have a program that runs via crontab every five minutes. It polls a
> Box.com folder for files and, if any are found, it copies them locally and
> performs a computation on them that can exceed five minutes. It pushes the
> results back up to Box. (Box.com ensures that only complete files are
> visible when I poll.) Files are dropped into this Box.com folder rarely,
> but to ensure a good customer experience I do not want to set my crontab to
> run less frequently. My hardware cannot support multiple simultaneous
> computations.
>
> I have written a piece of code to detect if more than 1 instance of my
> program is running, and I put this code into a separate module (support.py)
> so that other programs can use it.
>
> support.py contains:
>
> --------------------------------------------------------------------------------
> import sys
> def check(calling_program):
>     import psutil
>     # some logic here to count
>     # count = N
>     if count > 1:
>         print(f"I was called by {calling_program}.")
>         sys.exit()
> if __name__ == "__main__":
>     check()
>
> --------------------------------------------------------------------------------
>
> actual-program.py contains:
>
> --------------------------------------------------------------------------------
> import support.py
> support.check(__file__)
> # Poll, and if files download, perform expensive computations, push results
>
> --------------------------------------------------------------------------------
>
> To me it would be more elegant to be able to do something like this:
>
> def check():
>     # Something here that tells me the name of the calling program
>     import psutil
>     # ...
>
> And then the calling program just does:
> support.check()
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list