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):
Sleep is correctly imported at the top, there are sleep calls before this function runs that execute correctly.
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:
- I tried some things recommended in the comments: Commented out the self.setLevelCallbacks() line before the sleep, still exited
- Used sys.stdout.flush() after every print
- Checked with htop and saw no abnormal behaviour
- Printed a line right before the sleep and that shows.
That isn't how you sleep, You need to import the time module.
About python sleep
Python docs on Sleep
You can also use python Asynchronous I/O