title: Fastest way to detect if a process is running
body: <p>I need my object &quot;Launcher&quot; to detect if its associated process is running or not.
  My initial solution was to simply run a for loop using psutil</p>
  <pre><code>def Launcher(self, processName):
      self.processName=processName
  
  def process_up(self, attempts=0):
          if attempts &lt;= 3:
              try:
                  if self.processName in (p.name() for p in psutil.process_iter()):
                      return True
                  else:
                      return False
                  
              except:
                  self.process_up(attempts=1)
          else:
              logging.error(&quot;Psutil Fatal Error. Unable to check status of process {}&quot;.format(self.processName))
              return False
  </code></pre>
  <p>The recursion being used for the very rare case where a process p is detected in the for loop but dies before .name() is called.</p>
  <p>Anyway, it all sounded fine and dandy until I tested it with all my processes (about 40 of them, so 40 Launchers are running) and the issue is that running this loop takes about 0,1 seconds, which translates into a grand total of ~4 seconds .</p>
  <p>However, I need to aim at &lt;1 second. What are other super fast ways to find if a given process is running? I don't need to know anything about the process (I don't care about its pid or name), just if it's up or not.</p>
  <p>As a side note: I cannot use multithreading or any kind of parallelism. I have to run these Launchers sequentially.</p>
  <p>EDIT: I've tried the following code as well, which was definitely better performance wise:</p>
  <pre><code>def process_up(self):
      try:
          call = subprocess.check_output(&quot;pgrep -f '{}'&quot;.format(self.processName), shell=True)
          return True
      except subprocess.CalledProcessError:
          return False
  </code></pre>
  <p>Now the code runs in ~2 seconds, but it's still too much</p>