Python check if a process is running or not 
Question: I am trying to create a python script which I will later run as a service. Now
I want to run a particular part of the code only when iTunes is running. I
understand from some research that polling the entire command list and then
searching for the application for that list is expensive.

I found out that Programs in unix based operating systems create a lock file
to notify that a program is currently running. So we can use
os.stat(location_of_file) to check if the file exists to determine if a
program is running or not.

Is there a similar lock file created in windows ?

If not what are the various ways in python by which we can determine if a
process is running or not ?

I am using python 2.7 and iTunes COM interface.


Answer: You can not rely on lock files in Linux or Windows. I would just bite the
bullet and iterate through all the running programs. I really do not believe
it will be as "expensive" as you think.
[psutil](http://code.google.com/p/psutil/) is an excellent cross-platform
python module cable of enumerating all the running programs on a system.

    
    
    import psutil    
    "someProgram" in [psutil.Process(i).name for i in psutil.get_pid_list()]
    

