Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/huy-dna/pasync
An async runtime for Python
https://github.com/huy-dna/pasync
Last synced: 22 days ago
JSON representation
An async runtime for Python
- Host: GitHub
- URL: https://github.com/huy-dna/pasync
- Owner: Huy-DNA
- License: apache-2.0
- Created: 2024-05-28T16:18:14.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-05-29T03:43:26.000Z (5 months ago)
- Last Synced: 2024-05-29T07:38:43.599Z (5 months ago)
- Language: Python
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pasync - A simple event loop for async python
[![PyPI Version](https://img.shields.io/pypi/v/pasync.svg)](https://pypi.python.org/pypi/pasync)This minimal package is mainly used to explore implementing an async runtime in Python.
## Installation
```bash
pip install pasync
```## 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
```py
async def gen_num(n):
return n
with Runner() as runner:
assert runner.run_task(gen_num(1)) == 1
assert runner.gather(gen_num(1), gen_num(2)) == [1, 2]
```* Non-blocking runner
```py
async def gen_num(n):
return n
with Runner(non_blocking = True) as runner:
assert runner.run_task(gen_num(1)) == None # Not support returning results yet
assert runner.gather(gen_num(1), gen_num(2)) == None # Not support returning results yet
```### Promise
`Promise` is intended to simulate `Promise` in js. Its usage and semantics should be the same.
`Promise` itself is an awaitable, so it can be passed to `Runner.run_task`
`Promise.then` and `Promise.catch` return another promise.
## 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