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

An asynchronous experiment

Let's try an experiment. Maybe you've wondered how version4 (AsyncIO) is a bit like our version1 (event-loop) code, only it's been refactored into classes just like the version2 (threaded) code. So, couldn't we just refactor the code in the version1 while loop into classes, create and call a function them (for example, run()) in the while loop, and not bother with all the asynchronous stuff and its extra library and syntax?

Let's try. You will find a version just like this in the chapter12/version5_eventloop2 folder. Try running this version, and see what happens. You'll find that the first LED blinks, the second one is always on, and that the button and potentiometer do not work.

Can you work out why?

Here's the simple answer: in main.py, once the first LED's run() function is called, we're stuck in its while loop forever!

The call to sleep() (from the time library) does not yield control; it just...

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