psutil exception error - No such process

Viewed 2361

I have two independent Python scripts on a Raspberry Pi that are asynchronously accessing the RPi camera by calling the raspistill os function. I thought I had a function working that could check to see if the raspistill process was running, and if so, would wait until it cleared such that it could run. In both scripts, I included the following function:

from time import sleep
import psutil

def camFree():
    for pid in psutil.pids():
        p = psutil.Process(pid)
        if(p.name() == 'raspistill'):
            return False
    return True

I then put the following call to camFree in my code just before the raspistill call as follows:

while(camFree() is False):           # wait until camera is not being used
    time.sleep(1)

I have this script running every minute to take a picture, and have the output of the script redirected to a log file. Most of the time, I have no problems with the script running. However, occasionally, and not necessarily when the other camera script is running, I get errors as follows:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/psutil/_pslinux.py", line 1369, in wrapper
    return fun(self, *args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/psutil/_pslinux.py", line 1546, in create_time
    values = self._parse_stat_file()
  File "/usr/local/lib/python3.4/dist-packages/psutil/_common.py", line 337, in wrapper
    return fun(self)
  File "/usr/local/lib/python3.4/dist-packages/psutil/_pslinux.py", line 1408, in _parse_stat_file
    with open_binary("%s/%s/stat" % (self._procfs_path, self.pid)) as f:
  File "/usr/local/lib/python3.4/dist-packages/psutil/_pslinux.py", line 190, in open_binary
    return open(fname, "rb", **kwargs)
FileNotFoundError: [Errno 2] No such file or directory: '/proc/15538/stat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/psutil/__init__.py", line 428, in _init
    self.create_time()
  File "/usr/local/lib/python3.4/dist-packages/psutil/__init__.py", line 754, in create_time
    self._create_time = self._proc.create_time()
  File "/usr/local/lib/python3.4/dist-packages/psutil/_pslinux.py", line 1380, in wrapper
    raise NoSuchProcess(self.pid, self._name)
psutil.NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=15538)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/projects/daytimelapse/daytimelapse.py", line 189, in <module>
    while(camFree() is False):           # wait until camera is not being used
  File "/home/pi/projects/daytimelapse/daytimelapse.py", line 108, in camFree
    p = psutil.Process(pid)
  File "/usr/local/lib/python3.4/dist-packages/psutil/__init__.py", line 401, in __init__
self._init(pid)
  File "/usr/local/lib/python3.4/dist-packages/psutil/__init__.py", line 441, in _init
    raise NoSuchProcess(pid, None, msg)
psutil.NoSuchProcess: psutil.NoSuchProcess no process found with pid 15538

Any help would be appreciated! Thanks, Dave

0 Answers
Related