Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsync replaces event loop for main thread #6

Closed
and-semakin opened this issue Feb 25, 2019 · 3 comments
Closed

unsync replaces event loop for main thread #6

and-semakin opened this issue Feb 25, 2019 · 3 comments

Comments

@and-semakin
Copy link
Contributor

I don't know whether it's ok or not, but import unsync changes event loop for current thread, so event loops before import and after are not the same. It potentially can cause problems, because any regular non-unsync coroutine will run on unsync event loop.

In [1]: import asyncio                                                                                                                                                                                             

In [2]: original_loop = asyncio.get_event_loop()                                                                                                                                                                   

In [3]: from unsync import unsync                                                                                                                                                                                  

In [4]: current_loop = asyncio.get_event_loop()                                                                                                                                                                    

In [5]: original_loop == current_loop                                                                                                                                                                              
Out[5]: False

In [6]: current_loop == unsync.loop                                                                                                                                                                                
Out[6]: True
@and-semakin
Copy link
Contributor Author

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?

@alex-sherman
Copy link
Owner

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.

@and-semakin
Copy link
Contributor Author

and-semakin commented Feb 26, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants