Skip to main content

Async generators for humans

Project description

# Agent: Async generators for humans

**agent** provides a simple decorator to create python 3.5 [asynchronous iterators](https://docs.python.org/3/reference/compound_stmts.html#async-for) via `yield`s

## Examples

Make people wait for things for no reason!
```python
import agent
import asyncio

@agent.gen # Shorthand decorator
def wait_for_me():
yield 'Like '
yield from asyncio.sleep(1)
yield 'the line '
yield from asyncio.sleep(10)
yield 'at '
yield from asyncio.sleep(100)
yield 'the DMV'

async for part in wait_for_me():
print(part)
```

Paginate websites in an easy asynchronous manner.
```python
import agent
import aiohttp

@agent.async_generator
def gen():
page, url = 0, 'http://example.com/paginated/endpoint'
while True:
resp = yield from aiohttp.request('GET', url, params={'page': page})
resp_json = (yield from resp.json())['data']
if not resp_json:
break
for blob in resp_json['data']:
yield blob
page += 1

# Later on....

async for blob in gen():
# Do work
```


**The possibilities are endless!**

For additional, crazier, examples take a look in the [tests directory](tests/).


## Get it

```bash
$ pip install -U agent
```

## Caveats

`yield from` syntax must be used as `yield` in an `async def` block is a syntax error.

```python
async def generator():
yield 1 # Syntax Error :(
```

`asyncio.Future`s can not be yield directly, they must be wrapped by `agent.Result`.


## License

MIT licensed. See the bundled [LICENSE](LICENSE) file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agent-0.1.2.tar.gz (3.5 kB view details)

Uploaded Source

File details

Details for the file agent-0.1.2.tar.gz.

File metadata

  • Download URL: agent-0.1.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for agent-0.1.2.tar.gz
Algorithm Hash digest
SHA256 75e2654c40fd149c8f19f97da36d1f3818750571e6f1e734e1fb397d56a19e66
MD5 f09795340e357f7057550f7085d14edf
BLAKE2b-256 85693586641905a917f4929d584a79ac64f6df842f4e3ee51301643a73a8196e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page