This document provides an overview of coroutines in Python. It defines coroutines as components that allow non-preemptive multitasking and multiple entry points through suspending and resuming execution. Coroutines are compared to subroutines and generators. Generators in Python are described as frozen stack frames that yield values. Decorators are introduced as callables that modify function definitions. The document discusses how coroutines address issues with threads in Python by providing lightweight concurrency with less memory usage and startup costs compared to threads.
4. - Components that generalize subroutines for non-
preemptive multitasking.
- Allowing multiple entry points for suspending and
resuming execution at certain locations.
- Well sutied such as cooperative tasks, execptions,
event loop, iteratios, infinite lists and pipes.
- The term coroutine was coined by Melvin Conway in
1958.
Coroutine ?
4
5. Subroutines
- Begins at the start.
- Once a subroutine exits, it is finished.
- Instance of a subroutine only returns once.
- Does not hold state between invocations.
- Requires only a single stack that can be preallocated
at the start of program execution.
Comparison with subroutines
5
6. Coroutines
- Can exit by calling other coroutines, which may later
return to the point where they were invoked in the
original coroutine
- Holds state and varies between invocations.
- Can be multiple instances of a given coroutine at once.
- Able to call on other coroutines as peers.
- Creation can be done cheeply by preallocating stacks
or caching previously allocated stacks.
Comparison with subroutines
6
7. Comparison with subroutines
7
The queue is then completely filed
or emptied before yielding control
to the other coroutine using the yi
eld command.
The further coroutines calls are st
arting right after the yield, in the o
uter coroutine loop.
8. Coroutines
- Can yield multiple times.
- Suspending their execution.
- Allowing re-entry at multiple entry points.
- Can control where execution continues after they yield.
Comparison with generators
8
9. Generators
- Also known as semi-coroutines.
- Can’t control where execution continues after they yield.
Transferring control back to the generator’s caller.
- The yield statement in a generator does not specify a
coroutine to jump to, but rather passes a value back to a
parent routine.
- However, it is still possible to implement coroutines.
Comparison with generators
9
10. Comparison with generators
10
Does the same operation as the
previous example of coroutine.
However, there are differences i
n the role of yield.
The yield statement in a generat
passes a value back to a ‘dispat
cher’ subroutine.
12. - Special routine that can be used to control the
iteration behavior of loop.
- In fact, all generators are iterators.
- A generator is very similar to a function that returns an
array.
- Building an array containing all the values and returning
them all at once.
- However, a generator yields the values one at a time.
- In short, a generator looks like a function but behaves
like an iterator.
Generator ?
12
13. - Generators are usually invoked inside loops.
- Generators compute their yielded values only on demand.
- They are useful for representing streams.
- Such as sequences that would be expensive or impossible to
compute at once.
- These include infinite sequences and live data streams.
When using the generator
13
14. In python, a generator can be
thought of an iterator that
contains a frozen stack frame.
Whenever the iterator’s “next()”
method called, python resumes
the frozen frame, which
executes normally until the next
yield statement is reached.
The generator’s frame is then
frozen again, and the yielded
value is returned to the caller.
Generator in python
14
16. - An object that enables a programmer to traverse a
container, particularly lists.
- Performs traversal and also gives access to data
elements in a container, but does not perform iteration.
Iterator ?
16
17. - One way of implementing iterators is to use a
restricted form of coroutine, known as a generator.
- A generator coroutine can yield values to its caller
multiple times, instead of returning just once.
- There are subtle differences and distinctions in the
use of the terms “generator” and “iterator”, which vary
between authors and languages.
Iterator or generator
17
18. Iterators in python are a
fundamental part of the
language.
And in many cases go unseen
as they are implicitly used.
Iterator in python
18
20. - Not to be confused with the “Decorator pattern” .
- “Decorator pattern” is a design pattern used in statically
typed object-oriented programming languages to allow
functionality to be added to objects at run time.
- Any callable python object.
- That is used to modify a function, method or class definition.
- Passed the original object being defined and modified object,
which is then bound to the name in the definition.
Decorator ?
20
21. - Inspired in part by “Java annotations”.
- Have a similar syntax.
- Syntax is pure syntactic sugar.
- Using “@” as the keyword.
Decorator ?
21
22. They enhance the action of the
function or method they decorate.
Canonical uses of function
decrators are for create class
method or static methods,
adding function attributes,
tracing, setting pre- and
postconditions, and
synchronisation.
Decorator in python
22
23. - In python prior to version 2.6, decorators apply to
functions and methods.
- But not to classes.
- Class decorators are supported starting with python 2.6.
- PEP 3129
Decorator in python
23
25. - An entity which supports all the operations generally
available to other entities.
- These operations typically include being passed as an
argument, returned from a function, and assigned to a
variable.
- The concept of first- and second- class objects was
introduced by Christopher Strachey in the 1960s.
- But contrasted real numbers and procedures in ALGOL.
First-Class citizen ?
25
26. - The simplest scalar data types are nearly always first-
class.
- Such as integer and floating-point numbers.
- In many older languages, arrays and strings are not
first-class
- They cannot be assigned as objects or passed as
parameters to a subroutine.
- C appears to support assignment of array pointers, but in
fact these are simply pointers to the array’s first element,
and again do not carry the array’s size.
Examples
26
28. - Threads give python programmers a way to run
multiple functions seemingly at the same time.
- But there are three big problems with threads.
Thread in python
28
29. 1. They require special tools to coordinate with each
other safely.
- Use “lock’ to prevent data race.
- Use “queue” to coordinate work between threads.
- This makes code that uses threads harder to reason about
than procedural, single-threaded code.
- This complexity makes threaded code more difficult to
extend and maintain over time.
Thread problems in python
29
30. 2. Threads require a lot of memory, about 8MB per
executing thread.
3. Threads are costly to start.
- If you want to constantly be creating new concurrent
functions and finishing them, the overhead of using
threads becomes large and slows everything down.
Thread problems in python
30
31. - Python can work around all thread issues with
coroutines.
- Coroutines let you have many seemingly simultaneous
functions in your python programs.
- The cost of starting a generator coroutine is a
function call.
- They each use less than 1KB of memory until they’re
exhausted.
Coroutine in python
31
32. Python 2.5 implements better support for coroutine-like
functionality, based on extended generators (PEP 342)
Python 3.3 improves this ability, by supporting delegating
to a subgenerator (PEP 380)
Python 3.4 introduces a comprehensive asynchronous
I/O framework as standardized in PEP 3156, which
includes coroutines that leverage subgenerator
delegation
Python 3.5 introduces explicit support for coroutines with
async/await syntax (PEP 0492)
Coroutine in python
32
34. t h a n k y o u
Copyright 2016 Penta Security Systems Inc. All rights reserved.
KOREA
U.S.A.
JAPAN
Yeouido, Seoul www.pentasecurity.com (HQ)
Houston, Texas www.pentasecurity.com/en
Shinjuku-Ku, Tokyo
www.pentasecurity.co.jp