Coder Social home page Coder Social logo

sethmlarson / pycon-async-sync-poster Goto Github PK

View Code? Open in Web Editor NEW
22.0 4.0 1.0 910 KB

An example project which demonstrates how to use some new tools to more easily maintain a codebase that supports both async and synchronous I/O and multiple async libraries.

Home Page: https://sethmlarson.dev

License: Creative Commons Zero v1.0 Universal

Python 100.00%
async asyncio trio curio python pycon2020

pycon-async-sync-poster's Introduction

Designing Libraries for Async and Synchronous I/O

An example project which demonstrates how to use some new tools to more easily maintain a codebase that supports both async and synchronous I/O and multiple async libraries.

Meet the Tools

The library itself is a massive contrived example that doesn't do anything useful. The important part is seeing the different libraries and constructions all working together!

How the Project is Structured

There are three different categories of code that go into creating a project that supports sync, asyncio, trio, etc:

Code that directly interacts with I/O APIs (sockets, threads, asyncio)

Code that directly interacts with individual APIs that are different across the sync, asyncio, and Trio live under backends/. The function get_backend() can either return a SyncBackend which uses a threadpool for parallelism and time.sleep or return a flavor of AsyncBackend depending on which library sniffio detects.

Code that needs to be async but doesn't directly interact with I/O APIs

This is where the bulk of your libraries public API code will probably live. Typically you will write structural code here which call into your Backend code written above.

You want to try to fit as much of your API code here as you can so you can benefit from unasync generating the synchronous half automatically. When writing this code you'll have to keep in mind what the resulting generated code will look like though.

Code that doesn't have I/O

Code that doesn't need to touch I/O at all like enums, dataclasses, helpers, etc. Also things like importing your AsyncAPI and SyncAPI to make them accessible to users.

Interesting Places to Look

The Library in Action

import asyncio
import sleepy

# === Asyncio ===

sleeper = sleepy.AsyncSleeper()

async def main_asyncio():
    await sleeper.sleep_a_lot(3)

asyncio.run(main_asyncio())

# === Trio ===
# python -m pip install trio

import trio

sleeper = sleepy.AsyncSleeper()

async def main_trio():
    await sleeper.sleep_a_lot(10)  

trio.run(main_trio)

# === Sync ===

sleeper = sleepy.SyncSleeper()

def main_sync():
    sleeper.sleep_a_lot(5) 

main_sync()

License

CC0-1.0

pycon-async-sync-poster's People

Contributors

sethmlarson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

krzemienski

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.