Code exits without error or exception at sleep

718 Views Asked by At

I'm running a Python code in a Raspberry Pi 4 which reads signals from certain pins and responds accordingly.

On this function, after multiple prints in between lines, I narrowed the problem to (apparently) a sleep() call.

def on_enter_sanitizing(self):
    try:
        print('Entered state: Sanitizing')
        self.setLevelLedCallbacks() #This function has a print at the end, which shows
        sleep(0.8)
        print("POST SLEEP") #This print doesn't show
        level = self.checkForLevel()
        print('Sanitizing with level: '+level)
        self.BUTTON_PLAY_OUT.Press()
        self.setErrorLedCallbacks()
        errors = False
        while True:
            sleep(0.3)
            if self.checkEndCondition():
                break
            if self.checkForErrors():
                errors = True
                break
        self.cancelLevelCallbacks()
        self.cancelErrorLedCallbacks()
        if errors:
            self.error_mid_san()
        else:
            print("SANITIZATION OK")
            self.sanitizationFinished()
    except Exception as e:
        print("ERROR")
        print(e)

The try/except block was added to check for an Exception, but it doesn't trigger the "except" block. At one point I also added a print just before the sleep, which also showed.

The terminal, aside from any output before that sleep shows nothing when exiting, just like any code would do when finishing execution.

As background, I have some callbacks to certain Raspberry pin edges (using the pigpio module) and certain Timer objects running in the background (from the threading module). There are no exit calls in the code, so there is no reason why the code wouldn't continue onto the next lines. Also, a very similar function to this runs at some point before in the code just fine.

I don't know how else I can debug the code. Any help is greatly appreciated and any additional information I will be happy to provide.

EDIT (As I answer comments):

  1. Sleep is correctly imported at the top, there are sleep calls before this function runs that execute correctly.

  2. Because it was requested in the comments, this is the code for setLevelCallbacks:

    def setLevelLedCallbacks(self):
      self.pin_callback(self.PIN_L1,self.LED_L1.asyncRead(),None)
      self.LED_L1.setCallback(Either=self.pin_callback)
      self.pin_callback(self.PIN_L2,self.LED_L2.asyncRead(),None)
      self.LED_L2.setCallback(Either=self.pin_callback)
      self.pin_callback(self.PIN_L3,self.LED_L3.asyncRead(),None)
      self.LED_L3.setCallback(Either=self.pin_callback)
      print("CALLBACK COMPLETE") #This shows
    

The callback is called once before it is set because I need an initial value before any edge.

EDIT 2:

  1. I tried some things recommended in the comments: Commented out the self.setLevelCallbacks() line before the sleep, still exited
  2. Used sys.stdout.flush() after every print
  3. Checked with htop and saw no abnormal behaviour
  4. Printed a line right before the sleep and that shows.
2

There are 2 best solutions below

15
On

That isn't how you sleep, You need to import the time module.

import time

time.sleep(0.8) # Sleep for 8 seconds

About python sleep

Python docs on Sleep

You can also use python Asynchronous I/O

2
On

Possibly you should check your remaining code, where you use yours Timers, if they wait correctly for thread executing mentioned code.

If this try clause does not catch exception, there could be possibility that thread is terminated or stopped by its parent. Also, if parent process finishes before this thread, it will not give the thread time to finish cleanly. It is common issue with multithreaded apps, as well as apps terminated from terminal using i.e. kill -9.