Skip to main content

Agent: Async generators for humans

agent provides a simple decorator to create python 3.5 asynchronous iterators via yields

Examples

Make people wait for things for no reason!

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.

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.

Get it

$ pip install -U agent

Caveats

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

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

asyncio.Futures can not be yielded directly, they must be wrapped by agent.Result.

License

MIT licensed. See the bundled LICENSE file for more details.

Release files for agent 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agent 0.1.3
File Size Uploaded
agent-0.1.3.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agent 0.1.3
File Interpreter ABI Platform
agent-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 6.2 kB

Release files / agent-0.1.3.tar.gz

Download URL agent-0.1.3.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a010882b158376b64d24f4574afbfab4a5bf3ec5c99ca9abc11bf5459ad186f4
BLAKE2b-256 checksum
How to use checksums
5b0a4b9b1de3e1c551d2cce73ff1fcd4c9d4b6c324b4d7b44f3918f0fcdf7a86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.9.16 Darwin/22.3.0

Release files / agent-0.1.3-py3-none-any.whl

Download URL agent-0.1.3-py3-none-any.whl
Size 3.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b2ee6b770fd72e35ff0c1a9d9c2002b77ef30138f62a2a3f4291492ff85d807d
BLAKE2b-256 checksum
How to use checksums
1308d9a4ea4eb58605465f1c5c3e502added359a86fa424a7f66901dd87bd096
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.9.16 Darwin/22.3.0

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page