A Closer Look at Python Generators Vs Threads: Gallery & Guide
Overview of Python Generators Vs Threads
Short summary is kind of: You can use both generators /co-routines or threads or multiprocessing to do your work The Python GIL will bite you for co-routines/ threads if anything involves hard computational work in pure Python (C-Extension like numpy can avoid that). As long as you mostly do I/O it doesn't matter so much.
Python Generators Vs Threads
As we can see from the illustration, Python Generators Vs Threads has many fascinating aspects to explore.
Generators vs . Coroutines: The Subtle Power-Up If you want to push this idea further, you'll eventually hit coroutines generator -like constructs that can receive data using .send(). While most developers ignore this feature, it opens up fascinating automation patterns like real-time event handling or cooperative multitasking without threads .
Python Generators Vs Threads
Thread Safety of Generators in Python 3
Python Generators Vs Threads
As we can see from the illustration, Python Generators Vs Threads has many fascinating aspects to explore.
TBH when one considers the GIL, nightmares of thread synchronization and overhead of spawning threads , their usefulness is limited. They usually result in sloppy code that's hard to debug and no more efficient than simply writing an event loop (or using async if you can deal with only running on recent Python versions).
Python generators, coroutines, itertools, and more GitHub
Python iterators and generators have almost the same behavior, but there are subtle differences, especially when the iterator/ generator is used in a multi-threaded application.