Skip to main content

An event loop for python async

Project description

pasync - A simple event loop for async python

This minimal package is mainly used to explore implementing an async runtime in Python.

Usage

Runner

The main construct that you'll often work with is Runner.

A runner is actually a wrapper around an event loop.

It comes with two flavors: non-blocking runner and blocking runner.

  • A blocking runner will run all the tasks till end, blocking the main thread. When given a set of tasks, it will refuse to accept any task in the meaning time.
  • A non-blocking runner will run all the tasks in another thread. Unlike a blocking runenr, it can accepts additional tasks in the meaning time.

By default, a runner is blocking.

  • Blocking runner

    async def gen_num(n):
      return n
    
    with Runner() as runner:
      assert runner.run(gen_num(1)) == 1
      assert runner.gather(gen_num(1), gen_num(2)) == [1, 2]
    
  • Non-blocking runner

    async def gen_num(n):
      return n
    
    with Runner(non_blocking = True) as runner:
      assert runner.run(gen_num(1)) == None                  # Not support returning results yet
      assert runner.gather(gen_num(1), gen_num(2)) == None   # Not support returning results yet
    

Pitfalls

  1. Currently, non-blocking runner cannot return results of tasks yet. This can be worked around by making the top-level task return None, while all the subtasks's results are consumed inside the top-level task

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

pasync-0.3.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pasync-0.3.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file pasync-0.3.1.tar.gz.

File metadata

  • Download URL: pasync-0.3.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for pasync-0.3.1.tar.gz
Algorithm Hash digest
SHA256 c7b9138e1706c5910684ad84593f3a9a3c9c1387a63dfe410e53c33b9185c1da
MD5 043470c1e76798dd6c8c921da7515820
BLAKE2b-256 249ea1ca8def1e5fac5d745acc5bfb301fc2123a55e3dc7a268383fbd0160a47

See more details on using hashes here.

File details

Details for the file pasync-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pasync-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for pasync-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9340284db516b75c9e16aedc234c553649098abfa80a4ce34112d64a28eb849c
MD5 1f2448aba471fa11e85fe7168f099e20
BLAKE2b-256 d21356a3f26072a582e4935e40c4eea11397c398220181cab2126da44b04fc0c

See more details on using hashes here.

Supported by

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