Giter Site home page Giter Site logo

Using Async Await? about code HOT 18 OPEN

Didza avatar Didza commented on June 6, 2024
Using Async Await?

from code.

Comments (18)

nomhoi avatar nomhoi commented on June 6, 2024 2

Please see my refactoring: https://github.com/nomhoi/cosmicpython-fastapi

from code.

hjwp avatar hjwp commented on June 6, 2024 2

for the record, it's fine by me. i'll see what i can do about the license...

from code.

nomhoi avatar nomhoi commented on June 6, 2024 1

All 13 chapters have been completed.

from code.

hjwp avatar hjwp commented on June 6, 2024 1

two reasons at the top of my mind:

  1. async tests are slower (they tend to start and stop an event loop, makes them an order of magnitude slower in my experience)
  2. (i haven't really thought this through or experimented with it enough but) i see async as a sort of marker for "here be side effects + IO", so i thought of it as a really nice way of separating out a "functional core" from an "imperative shell", in the actual syntax of the language - a bit like the IO Monad in haskell. so in the python world, i'm thinking that the challenge is to keep as much of the core logic (and its unit tests) in a non-async world, and keep the async as close to the edges as possible. but that may not be compatible with repository pattern and unit of work.

from code.

nomhoi avatar nomhoi commented on June 6, 2024 1
  1. async tests are slower (they tend to start and stop an event loop, makes them an order of magnitude slower in my experience)

I used in conftest.py:

@pytest.fixture(scope="session")
def event_loop():
    policy = asyncio.get_event_loop_policy()
    loop = policy.new_event_loop()
    yield loop
    loop.close()

Only one event loop will be created per a testing session:
https://pytest-asyncio.readthedocs.io/en/latest/reference/fixtures.html#event-loop
By default the scope is 'function'.

from code.

hjwp avatar hjwp commented on June 6, 2024

i actually started investigating it in a branch somewhere once but i didn't finish it.

the tricky bit is figuring out how far down the stack you are going to allow async/await to encroach. the pure domain code needs to be non-async, i would say. but what about the Repository and the UnitOfWork? Or the Service Layer? i didn't decide on a final answer for that...

from code.

Didza avatar Didza commented on June 6, 2024

Theoretically it should be possible based on my understanding, it looks like the repository, service layer handlers to the entry points will be async and external calls awaited. Will investigate this further and document my findings here.

from code.

nomhoi avatar nomhoi commented on June 6, 2024

There are many examples with asynchronous FastAPI for RealWorld project:
https://github.com/search?q=realworld+fastapi

from code.

Didza avatar Didza commented on June 6, 2024

Please see my refactoring: https://github.com/nomhoi/cosmicpython-fastapi

Thanks for making efforts in this direction will definitely be on the look out to see how you implement this including the message_bus as async. Thank you for sharing.

from code.

nomhoi avatar nomhoi commented on June 6, 2024

Thanks for making efforts in this direction will definitely be on the look out to see how you implement this including the message_bus as async. Thank you for sharing.

I intend to complete all chapters.

from code.

jalvespinto avatar jalvespinto commented on June 6, 2024

@nomhoi, tks for sharing this. While you are at it, it would be nice to consider issue #23

from code.

nomhoi avatar nomhoi commented on June 6, 2024

@nomhoi, tks for sharing this. While you are at it, it would be nice to consider issue #23

Ok, a bit later.

from code.

nomhoi avatar nomhoi commented on June 6, 2024

@nomhoi, tks for sharing this. While you are at it, it would be nice to consider issue #23

Is the problem in this test?
https://github.com/xtaje/code/blob/2f260cba7eae3c6467c14befddfa92cc6d003c52/tests/e2e/test_queue_race.py

from code.

jalvespinto avatar jalvespinto commented on June 6, 2024

I am not sure. If I understood it right the problem is when you have a server that don't spin up one thread per request. The reason been that the bus is a module variable and when in the same thread that would be a problem for the bus queue and the db session, since multiple requests would share the queue and session. In my case I am creating a new queue and uow on a per request basis, but I guess this is a problem for the tests on the project. I don't really know, because I am not running the projects code/tests.

from code.

hjwp avatar hjwp commented on June 6, 2024

Please see my refactoring: https://github.com/nomhoi/cosmicpython-fastapi

this is awesome! well done :)

if i get a bit more time, i want to investigate some more patterns around this. can we make it so that the repository is non-async somehow?? if you could somehow pre-load the state before entering the service layer, and have the outputs be some combination of state to modify (on a sqla session maybe?) plus events, then maybe the whole service layer could be sync somehow? it wouldn't work for the UoW, but, idk, the dream is, the fewer unit tests use async, the better.

from code.

jalvespinto avatar jalvespinto commented on June 6, 2024

@hjwp why do you want a non-async? I am asking just because I am going full async on a project that I use the archit from your book.

from code.

nomhoi avatar nomhoi commented on June 6, 2024

May be this will be usefull: https://returns.readthedocs.io/en/latest/pages/future.html

from code.

dbaber avatar dbaber commented on June 6, 2024

Are derivatives/forks like the Fastapi one even legally allowed on github because the license is CC-By-ND?

from code.

Related Issues (20)

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.