0

if I have two or more running python console applications at the same time of same application, but executed several times by hand or any other way.

Is there any method from python code itself to stop all extra processes, close console window and keep running only one

nikorio
  • 671
  • 4
  • 16
  • 28

1 Answers1

0

The solution I would use would be to have a lockfile created in the tmp directory.

The first instance would start, check for the existence of the file, create the file since it is not there, then run; the following instances will start, check for the existence of the file, then quit since it's there. The original instance would remove the lockfile as its last instruction. NOTE: If the app runs into an error and does not execute the instruction to remove the lockfile, you would need to manually remove it else the app will always see the file.

I've seen on other threads that some suggest using the ps command and look for your app's name, which would work; however, if your app will ever run on Windows, you would need to use tasklist.

BnVnSn
  • 155
  • 2
  • 5
  • Hello, I want run this application on windows, seems like I need to use tasklist, because reason why I need it, arises with windows, not on linux. So now I'm trying to find how to make it in python code, but still I can't find any basic direction – nikorio Dec 10 '16 at 15:57
  • http://stackoverflow.com/a/2241047/3550833 Giampaolo's answer is a start. Instead of `print` at the last line, just quit. – BnVnSn Dec 11 '16 at 04:41