Cooperative scheduler

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Cooperative scheduler

Post by pythoncoder » Thu May 19, 2016 8:09 am

I've made a number of improvements to the simple cooperative scheduler which was my first Pyboard project nearly two years ago. I mention it with some trepidation as the official way to implement cooperative scheduling is to use uasyncio, and I recommend using this wherever possible.

However uasyncio on the Pyboard is currently too slow for many applications. This solution supports millisecond-level scheduling on the Pyboard family. It employs a well-established Python paradigm using generators to implement coroutines. The technique is commonly termed 'microthreading' or 'lightweight threads' and will doubtless be familiar to other geriatrics like myself.

URL: https://github.com/peterhinch/Micropython-scheduler.git
Brief summary of improvements:

Proper documentation with a section for beginners.
Better and simpler scheduling algorithm.
Periodically (and optionally) runs garbage collection to reduce heap fragmentation.
Simplified API: yield can be used in a flexible way to determine when the thread will resume.
Threads can start, pause or kill other threads.
Design altered to facilitate porting to other MicroPython platforms. It now uses utime with pyb usage restricted to a single optional Pyboard-specific method.
Delay module supports scheduling of future callbacks or threads.
'minified' variant removed: superseded by frozen modules and frozen bytecode.

For existing users the improved API is almost entirely a superset of the old one. Most existing code should run without change. The library has been tested as frozen bytecode.
Peter Hinch
Index to my micropython libraries.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Cooperative scheduler

Post by SpotlightKid » Thu May 19, 2016 9:35 pm

Well, uasyncio on the pyboard never really took off, because there was no official implementation of the event loop that worked on the pyboard and documentation was virtually inexistent.

So, I applaud your initiative.

Post Reply