This module provides fake coroutines that are not coroutines but act like coroutines.
>>> from fake_coro import fake_coro, yield_
>>> @fake_coro
... def fib(limit):
... a, b = 1, 1
... while a < limit:
... yield_(a)
... a, b = b, a + b
>>> list(fib(10))
[1, 1, 2, 3, 5, 8]
Other features of classic coroutines are also supported:
>>> @fake_coro
... def my_coro():
... return 42
>>> next(my_coro())
Traceback (most recent call last):
...
StopIteration: 42
Also .send, .throw and .close:
>>> @fake_coro
... def average():
... total = count = 0
... num = yield_()
... while True:
... total += num
... count += 1
... num = yield_(total / count)
>>> coro = average()
>>> next(coro)
>>> coro.send(1)
1.0
>>> coro.send(6)
3.5
>>> coro.close()
>>> coro.send(7)
Traceback (most recent call last):
...
StopIteration
And sub-coroutines:
>>> from fake_coro import yield_from
>>> @fake_coro
... def chain(*iterables):
... for iterable in iterables:
... yield_from(iterable)
>>> list(chain(range(3), range(5)))
[0, 1, 2, 0, 1, 2, 3, 4]
Fake coroutines are implemented by threads and obviously perform much worse than native classic coroutines. However, normal functions invoked by fake coroutines can also yield like coroutines:
>>> @fake_coro
... def foo():
... bar()
... yield_(2)
>>> def bar():
... yield_(1) # bar is not a coroutine but can yield!
>>> list(foo())
[1, 2]
Metadata
Release files for fake-coro 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fake_coro-0.1.1.tar.gz | 4.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fake_coro-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.3 kB
Release files / fake_coro-0.1.1.tar.gz
| Download URL | fake_coro-0.1.1.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
272eb04157af66e04a1c18b8db3dceea333a7def77c203030a18ada0cd871113
|
|
BLAKE2b-256 checksum How to use checksums |
9f67f138b6f97c5ec793fbb9239cb4eac190d4a67925d02ec66650221d65a135
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.0 CPython/3.10.4 Linux/5.15.0-71-lowlatency
|
Release files / fake_coro-0.1.1-py3-none-any.whl
| Download URL | fake_coro-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
87c3165d2f41c5e9ab71baf477a4f32ff1e19602736e503ac48f2cb67cb63c21
|
|
BLAKE2b-256 checksum How to use checksums |
46c5c9bf8be53070d31fab31b9d0ec18e2a09efff4c29f02b60ed19fbce7ea8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.0 CPython/3.10.4 Linux/5.15.0-71-lowlatency
|