Giter VIP home page Giter VIP logo

pyasync's Introduction

Some experiments with asynchronous programming in Python, largely inspired by the following great resources:

The general idea is to take some very simple network application senarios, implement synchronous versions, and gradually make changes to turn them asynchronous exploring various techniques and tradeoffs along the way (threads, callback style, coroutines…) Finally, provide corresponding variants using asyncio’s event loop and APIs.

Fibonacci servers

The fibservers directory contains incrementals version of a simple TCP server computing Fibonacci numbers, gradully going from synchronous to async. Each version of the server comes with an equivalent implementation using asyncio’s event loop and APIs.

  • fibservsync.py

    Synchronous version. Handles one connexion at a time.

  • fibservthrd.py

    Introduce concurrency by handling each incoming connexion in a separate thread.

  • fibservcb.py

    First single-threaded async version, using callbacks and I/O polling with the standard library’s selectors module.

  • fibservcoro.py

    Use coroutines to implement tasks, driven by a simple event loop.

  • fibservcorothrd.py

    Asynchronous I/O handled by an event loop in the main thread, and computation of Fibonacci numbers delegated to a ThreadPoolExecutor.

Asyncio variants

Performance assessment

In addition to these, I adapted David Beazley’s client implementations to play around and test performance:

  • fibclientfast.py

    Repeatedly send short-time requests and monitor the requests-per-second rate.

  • fibclientslow.py

    Send comparatively CPU-demanding requests (computing Fib(33)) and monitor the time-per-request.

Using various combinations of these allows to get an idea of some tradeoffs involved when doing asynchronous programming in Python.

Here are the results of the tests performed on my machine:

Server version1 fast2 fast1 slow2 slowfast + slowfast + slow
req/sreq/ss/reqs/req(fast) req/sslow (s/rec)
Synchronous38000X1XXX
Threads340001700012.11001
Callbacks33000200001.12.111.1
Coroutines32000160001.12.211.1
Coroutines and threads800040001.12.2301.1
Asyncio
Callbacks30000180001211
Coroutines2000090001211
Coroutines and threads5000300012501
Streams4500260012401

Presentation

Some notes in French of a short presentation I gave on the topic, and the corresponding reveal.js export (generated by org-reveal) lie in the presentation directory, along with some drawings.

Simplified event loop

While reading asyncio code, I started out to implement a simplified version of the event loop, tasks and futures, while mimicking the API. It tries to capture the essential logic, while overlooking error handling and many other subtleties. This work in progress is in the eventloop directory.

Web clients

The webclients directory follows the same incremental approach, focusing on the simple task of concurrently fetching a list of URLs. Work in progress, also integrating some Tornado examples.

pyasync's People

Contributors

kwentine 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.