GithubHelp home page GithubHelp logo

pasync's Introduction

pasync - A simple event loop for async python

PyPI Version

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

Installation

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

    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

    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

pasync's People

Contributors

huy-dna avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.