Giter Club home page Giter Club logo

asynctranslator's Introduction

ASyncTranslator

A package to transform your sync methods to async methods and to turn async methods to sync methods.

🤩 ✨ FINALLY TYPED EDITION!!! ❤️ 🤌

This is an heavy inspiration from Django's implementation. Please see https://github.com/django/asgiref/blob/30d891fab0a7caa265bda0cbe04bb35a00a3b3b9/asgiref/sync.py for original reference.

Show me an example, pretty please.

Here you go!

To transform from an async to sync method:

from asynctranslator import async_to_sync

async def my_async_func(food_name: str) -> int:
    print(f"{food_name} are my favorite!")
    return 42

def main():
    final_func = async_to_sync(my_async_func)
    result = final_func("Cookies")
    print(result)  # We got "42"!

main()

And now, to transform from a sync to async method:

import asyncio
from asynctranslator import sync_to_async

def my_async_func(food_name: str) -> int:
    print(f"{food_name} are my favorite!")
    return 42

async def main():
    final_func = sync_to_async(my_async_func)
    result = await final_func("Cookies")
    print(result)  # We got "42"!

asyncio.run(main())

But you can also use it as a decorator!

import asyncio
from asynctranslator import sync_to_async

@sync_to_async
def my_async_func(food_name: str) -> int:
    print(f"{food_name} are my favorite!")
    return 42

async def main():
    result = await my_async_func("Cookies")
    print(result)

asyncio.run(main())

asynctranslator's People

Contributors

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