Advanced Python Training
In Chicago with David Beazley

  • Practical Python Programming
  • Advanced Python Mastery
  • Write a Compiler
  • Click here for more details!

    Generators: The Final Frontier

    Copyright (C) 2014
    David M. Beazley

    Presented at PyCon'14, April 10, 2014, Montreal.


    This tutorial discusses advanced uses of using generators to alter program control flow, explode brains, and exponentially increase your job security. Topics include context managers, inlined futures, concurrency, asyncio, actors, compilers, and more.

    Support Data Files

    The following file contains some supporting data files that are used by the various code samples. Download this to your machine to work the examples that follow. This download includes the presentation slides and all of the code sample below.

    Background Material

    This tutorial is part 3 of a trilogy of tutorials involving generators and coroutines. Although it stands on its own, you may want to review the first two parts to get the bigger picture:

    Code Samples

    Here are various code samples that are used in the course. You can cut and paste these to your own machine to try them out. The order in which these are listed follow the course outline. You'll need Python 3.4.

    Part 2 : And Now For Something Completely Different

    • Sample implementation of context managers using generators. Similar to contextlib standard library.
    Part 3 : Call me Maybe

    Part 4: Yield From Yield From Yield From Future

    Part 5: GIL

    • Fibonacci numbers in a process pool and inlined futures.
    • Performance test, side-stepping the GIL (it works!)
    • Performance test with a different thread execution model.

    Part 6 : Fake it Until You Make It (Actors)

    • Simple actor example.
    • Recursive ping-pong (broken).
    • Recursive ping-pong using threads.
    • Recursive ping-pong with simple message queue and runner.

    Part 7: A Terrifying Visitor

    • A simple compiler and tree traversal using the visitor pattern.
    • Non-recursive tree traversal using generators (insane).