Git Product home page Git Product logo

Comments (3)

alex-sherman avatar alex-sherman commented on May 19, 2024 1

I made a PR for this: #8 but I'm concerned that this is not as simple as it seems, I'm curious to know in what cases the main thread's event loop possibly be used by an asyncio Future/coroutine that might be awaited by an @unsync function.

It appears that coroutines started in the main thread will still get executed in the event loop of the coroutine that awaits it. I'm pretty sure that means this change is safe, but I'm going to spend some more time looking into any other ways a coroutine/Future from asyncio could get executed in main thread's event loop while being awaited by an @unsync function.

from unsync.

and-semakin avatar and-semakin commented on May 19, 2024

I expect unsync to work in another thread with its own event loop and don't make a mess with main thread and loop. Don't know how to implement it. Any ideas?

from unsync.

and-semakin avatar and-semakin commented on May 19, 2024

It's ok that coroutine will get executed in the event loop of another coroutine that awaits it. But simple async functions that is not marked with @unsync decorator should not be executed in the unsync event loop. Here is what I mean in code:

async def async_coro_returning_event_loop() -> asyncio.AbstractEventLoop:
    await asyncio.sleep(0.001)
    loop = asyncio.get_event_loop()
    return loop

@unsync
async def unsync_coro_returning_event_loop() -> asyncio.AbstractEventLoop:
    return await async_coro_returning_event_loop()

main_loop = asyncio.get_event_loop()
unsync_loop = unsync.loop

assert main_loop != unsync_loop
assert main_loop == await async_coro_returning_event_loop()
assert unsync_loop == unsync_coro_returning_event_loop().result()

It seems that #8 fixes issue -- unsync does asyncio.set_event_loop in its own separate thread.

from unsync.

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.