Skip to content

Commit

Permalink
add the aiter_sync() function (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur authored Sep 7, 2023
1 parent 0e48ebc commit e8b9af5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ way Python's `asyncio` module does things.

- Helper tools for controlling coroutine execution, such as `CoroStart` and `Monitor`
- Utility classes such as `GeneratorObject`
- Coroutine helpers such as `await_sync()`, `coro_iter()` and the `awaitmethod()` decorator
- Coroutine helpers such as `await_sync()`, `aiter_sync()`, `coro_iter()` and the `awaitmethod()` decorator
- Scheduling helpers for `asyncio`, and extended event-loop implementations
- _eager_ execution of Tasks
- Limited support for `anyio` and `trio`.
Expand Down Expand Up @@ -109,7 +109,7 @@ just as would happen if it were directly turned into a `Task`.

`func_eager()` is a decorator which automatically applies `coro_eager()` to the coroutine returned by an async function.

## `await_sync()` - Running coroutines synchronously
## `await_sync(), aiter_sync()` - Running coroutines synchronously

If you are writing code which should work both synchronously and asynchronously,
you can now write the code fully _async_ and then run it _synchronously_ in the absence
Expand Down Expand Up @@ -195,6 +195,22 @@ async def sync_client(sync_callback):
Using this pattern, one can write the middleware completely async, make it also work
for synchronous code, while avoiding the hybrid function _antipattern._

### `aiter_sync()`

A helper function is provided, which turns an `AsyncIterable` into
a generator, leveraging the `await_sync()` method:

```python
async def agen():
for v in range(3):
yield v

assert list(aiter_sync(agen())) == [1, 2, 3]
```

This is useful if using patterns such as `GeneratorObject` in a synchronous
application.

## `CoroStart`

This class manages the state of a partially run coroutine and is what what powers the `coro_eager()` and `await_sync()` functions.
Expand Down

0 comments on commit e8b9af5

Please sign in to comment.