Coder Social home page Coder Social logo

tiangolo / asyncer Goto Github PK

View Code? Open in Web Editor NEW
1.4K 16.0 44.0 756 KB

Asyncer, async and await, focused on developer experience.

Home Page: https://asyncer.tiangolo.com/

License: MIT License

Python 97.58% Shell 2.42%
python async asyncio trio anyio

asyncer's Introduction

Asyncer

Asyncer, async and await, focused on developer experience.

Test Publish Coverage Package version


Documentation: https://asyncer.tiangolo.com

Source Code: https://github.com/tiangolo/asyncer


Asyncer is a small library built on top of AnyIO.

Asyncer has a small number of utility functions that allow working with async, await, and concurrent code in a more convenient way under my (@tiangolo - SebastiΓ‘n RamΓ­rez) very opinionated and subjective point of view.

The main goal of Asyncer is to improve developer experience by providing better support for autocompletion and inline errors in the editor, and more certainty that the code is bug-free by providing better support for type checking tools like mypy.

Asyncer also tries to improve convenience and simplicity when working with async code mixed with regular blocking code, allowing to use them together in a simpler way... again, under my very subjective point of view.

🚨 Warning

This small library only exists to be able to use these utility functions until (and if) they are integrated into AnyIO.

It will probably take some time for that to happen (or to be decided if it will be included or not).

So I made this to be able to use these ideas right now. πŸ€“

Can I Use It?

Yes πŸŽ‰ (but continue reading).

You can use this and evaluate the library API design I'm proposing. It will probably be useful to know if it works and is useful for you (I hope so).

But still, consider this lab material, expect it to change a bit. πŸ§ͺ

If you use it, pin the exact Asyncer version for your project, to make sure it all works.

Have tests for your project (as you should, anyway). And upgrade the version once you know that the new version continues to work correctly.

Still, it's just 4 functions, so there's not much to change, if you had to refactor your code to update something it would not be much.

And if you don't want to add asyncer as a dependency to your project, you can also just copy the main file and try out those functions, it's quite small (but in that case you won't get updates easily).

Requirements

As Asyncer is based on AnyIO it will be also installed automatically when you install Asyncer.

Installation

$ pip install asyncer
---> 100%
Successfully installed asyncer anyio

How to Use

You can read more about each of the use cases and utility functions in Asyncer in the tutorial.

As a sneak preview of one of the utilities, you can call sync code from async code using asyncify():

import time

import anyio
from asyncer import asyncify


def do_sync_work(name: str):
    time.sleep(1)
    return f"Hello, {name}"


async def main():
    message = await asyncify(do_sync_work)(name="World")
    print(message)


anyio.run(main)

Asyncer's asyncify() will use AnyIO underneath to do the smart thing, avoid blocking the main async event loop, and run the sync/blocking function in a worker thread.

Editor Support

Everything in Asyncer is designed to get the best developer experience possible, with the best editor support.

  • Autocompletion for function arguments:

  • Autocompletion for return values:

  • Inline errors in editor:

  • Support for tools like mypy, that can help you verify that your code is correct, and prevent many bugs.

License

This project is licensed under the terms of the MIT license.

asyncer's People

Contributors

dependabot[bot] avatar estebanx64 avatar giladsheffer avatar jonasks avatar khiemdoan avatar kludex avatar markparker5 avatar michaeloliverx avatar pre-commit-ci[bot] avatar realfranco avatar sanders41 avatar tiangolo avatar vrslev avatar windson avatar zhymabekroman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

asyncer's Issues

πŸš€ Roadmap

Description

This is a tentative roadmap, I will update it as things evolve. Some things might be discarded, others might be added later. I didn't want to make it fully public as it could raise expectations, but it seems it would be more beneficial for the community to know all the ideas and objectives.

Work on this is alternated (and sometimes mixed) with work on FastAPI, Typer, SQLModel, and others.

Answering questions, issues, and PRs is also intermixed with this, but as in some cases one of these features would solve several issues or questions, or potentially solve something done by one or more PRs, in many cases I focus on this a bit more than on answering specific issues, questions, PRs.

Maintenance

The word "maintenance" or "maintainer" doesn't have a very strict definition, and it probably varies a lot from perspective.

A lot of the work related to maintaining FastAPI is done by contributors by answering questions, reviewing PRs, etc.

You can help me ensure each existing PR is in shape (has tests, solves the problem, etc.). Make sure you filter out the translation PRs (most of them) unless you speak the language and can review them.

Security

When there are security issues, those are handled with the highest priority. Those are normally not handled in issues and PRs but in emails, it's not public until the security disclosure is made, in most cases (always, up to now) with the version that fixes them.

Roadmap

Now, here's the current high-level tentative roadmap:

  • Support for the latest AnyIO.
  • Docs for handling concurrency.
  • Docs in FastAPI.

All this is intermixed with reviews for PRs, issues, and discussions.

How to handle exceptions ?

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Asyncer documentation, with the integrated search.
  • I already searched in Google "How to X in Asyncer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Asyncer but to AnyIO.
  • I already checked if it is not related to Asyncer but to Trio.

Commit to Help

  • I commit to help with one of those options πŸ‘†

Example Code

import asyncer
import logging
from typing import Union

from fastapi import FastAPI

async def func1(name: str):
    return {"message": f"Hello {name} form func1"}


async def func2():
    return {"message": f"Hello {name} form func2"}


@app.get("/test")
async def knowledge_panel(name_str: str):
    async with asyncer.create_task_group() as task_group:
        try:
            soon_value1 = task_group.soonify(func1)(name=name_str)
        except Exception:
            logging.exception()
        try:
            soon_value2 = task_group.soonify(func2)(name=name_str)
        except Exception:
            logging.exception()
    data = [soon_value1.value, soon_value2.value]
    return data

Description

  • How to handle an exception like here soon_value2 will throw an error, then how could I catch the exceptions and return user only single data rather than crashing the whole program?

Operating System

Windows

Operating System Details

No response

asyncer Version

0.0.1

Python Version

Python 3.10.5

Additional Context

No response

Invalid images path in tutorial directory

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Asyncer documentation, with the integrated search.
  • I already searched in Google "How to X in Asyncer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Asyncer but to AnyIO.
  • I already checked if it is not related to Asyncer but to Trio.

Commit to Help

  • I commit to help with one of those options πŸ‘†

Example Code

-

Description

Hi @tiangolo.
Images path in tutorial directory is invalid. It seems all of them need a /doc in the beginning of their path.
In #7, I've fixed one of them in asyncify section of the tutorial page. Do you think other cases need to be resolved in the same PR?
Thanks for this awesome library. πŸ₯³

Operating System

Linux

Operating System Details

No response

asyncer Version

Python Version

Additional Context

No response

Autocompletion doesn't seem to work for PyCharm

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Asyncer documentation, with the integrated search.
  • I already searched in Google "How to X in Asyncer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Asyncer but to AnyIO.
  • I already checked if it is not related to Asyncer but to Trio.

Commit to Help

  • I commit to help with one of those options πŸ‘†

Example Code

from datetime import datetime

from asyncer import asyncify


def foobar(x: int, y: 'str') -> datetime:
    return datetime(int(y), x, 1)


async def main():
    x = await asyncify(foobar)()

Description

asyncer looks awesome yet again. πŸŽ‰

Unless I'm missing something(?), I don't seem to get auto-completion help on pycharm.

image

Might be worth:

  • putting a note in the docs about "auto-completion on vscode"
  • thinking about a way to get this to work on pycharm - I assume it would require a plugin?
  • bugging @pauleveritt about whether pycharm have a plan to add support for stuff like this? - It wouldn't just be for this library, if pycharm could do clever inspection of type hints in decorators, it would open up lots of interesting applications to those of us wedded to pycharm

Operating System

Linux

Operating System Details

Ubuntu 21.04

asyncer Version

0.0.1

Python Version

3.9.5

Additional Context

No response

how do you get mypy working with paramspec... I cannot

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Asyncer documentation, with the integrated search.
  • I already searched in Google "How to X in Asyncer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Asyncer but to AnyIO.
  • I already checked if it is not related to Asyncer but to Trio.

Commit to Help

  • I commit to help with one of those options πŸ‘†

Example Code

n/a

Description

Hi tiangolo!

Looks like a nice lib. I have a lib called asyncify (https://github.com/dynamic-graphics-inc/dgpy-libs/tree/master/libs/asyncify) pip install asyncify that has an asyncify function. I cannot get my asyncify function to work with mypy and paramspec. do you have any suggestions.

Operating System

Linux, Windows, macOS

Operating System Details

No response

asyncer Version

n/a

Python Version

36+

Additional Context

This is a random question and I thought you might be interested. DM me if you are interested in taking over the asyncify name as it is imo a better name.

Discussion on function naming

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Asyncer documentation, with the integrated search.
  • I already searched in Google "How to X in Asyncer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Asyncer but to AnyIO.
  • I already checked if it is not related to Asyncer but to Trio.

Commit to Help

  • I commit to help with one of those options πŸ‘†

Example Code

import anyio
import asyncer

def sync_work(name: str):
    time.sleep(1)
    return f"Hello, {name}"

async def do_async_work(name: str):
    message = asyncer.run_sync(sync_work)(name=name)
    return message

async def main():
    message = await do_async_work(name="World")
    print(message)

asyncer.run(main)

Description

It's strange to name the three main functions, Why not use regular name?

  • asyncify
  • syncify
  • runnify

The first two unified can be understood, but runnify is always weird.

Why not

  • run_async
  • run_sync
  • run

Operating System

Windows

Operating System Details

No response

asyncer Version

0.0.1

Python Version

3.X

Additional Context

No response

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.