Reader small image

You're reading from  Practical Python Programming for IoT

Product typeBook
Published inNov 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838982461
Edition1st Edition
Languages
Right arrow

Preventing the main thread from terminating

It's common to see the use of signal.pause() or an equivalent construct in GPIO examples and programs: 

signal.pause() # Stops program from exiting.  # (8)

Line (8) prevents the main program thread from reaching its natural end, which under normal circumstances is where the program terminates.

Forgetting to add signal.pause() to the end of a GPIO-interfacing Python program is a common and often confusing mistake when starting out. If your program exits immediately after it's started, try adding signal.pause() at the end of your program as a first step.

We didn't need signal.pause() with our previous LED flashing examples. Here is why:

  • Our GPIOZero example (chapter02/led_gpiozero.py) used background=False in the LED constructor. This prevented our program from exiting by keeping the LED's thread in the foreground.
  • In the PiGPIO example (chapter02/led_pigpio.py), it's the while loop that...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Practical Python Programming for IoT
Published in: Nov 2020Publisher: PacktISBN-13: 9781838982461