woodmister
Posts: 18
Joined: Tue Mar 12, 2019 10:01 pm

How to check to see if a process is running via subprocess, and not do it if it already exists.

Wed Apr 10, 2019 2:13 am

Hey guys, more questions.
When I run the subprocess.Popen, is there a way to check and see if there is already one going?

I have lots several different files that could stream, or if i accidentally double-tap the screen, i don't want it to send the program to etch twice, I would like some protection for like a "if it's going, don't do again". Some of the stuff i'm trying and dealing with are below.

I'd love a way to know if it's going so I can change an indicator light on my tkinter screen to show it's "lasing" and then when it's done via another d.poll(), i'll show "done" as an indicator light.

Code: Select all

def stream_circle():
    global process
    process = subprocess.Popen(["python", "stream.py", "/home/pi/laseretcher/gcode/circle.gcode", "/dev/ttyUSB0"])
    if process.poll() is None:
        time.sleep(5)
        print("Lasering!")
        #printing_indicator()
    print("Finished Process")
   # rotate_table()

def stream_uvulogo():
        process = subprocess.Popen(["python", "stream.py", "/home/pi/laseretcher/gcode/uvulogo.gcode", "/dev/ttyUSB0"])
    while p.poll() is None:
        time.sleep(5)
    print("Finished Process")
    rotate_table()
    
def stream_stitch():
        process = subprocess.Popen(["python", "stream.py", "/home/pi/laseretcher/gcode/stitch.gcode", "/dev/ttyUSB0"])
    while p.poll() is None:
        time.sleep(5)
    print("Finished Process")
    rotate_table()
    
Thanks for your help in advance!

Andyroo
Posts: 3864
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: How to check to see if a process is running via subprocess, and not do it if it already exists.

Wed Apr 10, 2019 11:20 am

Three ways spring to mind.

The most complex would be to see if the process is running by using the ‘ps’ command and checking it’s output.

The second way would be for the process to write its PID to a file and delete this file when done.

The simpler way in Python is something like this (note true code):

Code: Select all

not running = true 

# In function to action button press / screen click
    If notrunning 
        notrunning = false
        call process and wait till it ends
        notrunning = true
        
Need Pi spray - these things are breeding in my house...

User avatar
MrYsLab
Posts: 353
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: How to check to see if a process is running via subprocess, and not do it if it already exists.

Wed Apr 10, 2019 1:01 pm

You can also use psutil https://psutil.readthedocs.io/en/latest ... #processes

Code: Select all

import psutil


def find_name(name):
    for proc in psutil.process_iter():
        try:
            pinfo = proc.as_dict(attrs=['pid', 'name'])
            if pinfo['name'] == name:
                return True
            else:
                continue
        except psutil.NoSuchProcess:
            pass
    return False


print(find_name('python'))
print(find_name('bogus'))

Paul Hutch
Posts: 370
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: How to check to see if a process is running via subprocess, and not do it if it already exists.

Wed Apr 10, 2019 1:24 pm

subprocess.Popen has a built-in method to determine if the subprocess is still running, Popen.poll().

In your code process.Popen.poll() will return the state of the process.

https://docs.python.org/3/library/subpr ... en-objects

woodmister
Posts: 18
Joined: Tue Mar 12, 2019 10:01 pm

Re: How to check to see if a process is running via subprocess, and not do it if it already exists.

Wed Apr 10, 2019 3:35 pm

Thanks guys. The check seems to be working perfectly. I just need to add in the "don't do it if it's working" part.


[codedef run_check():
global etching
if process.poll() is None:
print("Lasing")
etching = 1
#printing_indicator()
else:
print("Finished The Thing!")
etching = 0
return
# rotate_table()
root.after(2000, run_check) [/code]


THANKS!

Return to “Python”