aiorun icon indicating copy to clipboard operation
aiorun copied to clipboard

programatically stopping a loop

Open catchthemonster opened this issue 3 years ago • 2 comments

Hi, great library, thanks a lot... Documentation is good too! My issue is following: I do want to start two separate rest api services via corutines in async way. I have no issues setting up a handler to catch exceptions in event_loop. however I want to be able programmatically to stop the loop. anything after run(loop=loop) is not going to work! what is the way to actually stop the loop and consequently stop two rest api verticals....

Regards,

catchthemonster avatar Sep 13 '22 19:09 catchthemonster

It depends most strongly on the REST servers you're using. Each of the frameworks (aiohttp, fastapi, sanic, etc.) will have a slightly different way of shutting down. You will have to study the docs of your chosen framework to see how they want you to start up and shut down their server. Usually they (the frameworks) control the starting up and shutting down of the event loop, so aiorun probably can't help much here.

cjrh avatar Sep 14 '22 01:09 cjrh

Hi, Not really! uvicorn, fastAPI and other servers actually have nothing in the async world that actually gracefully shutdown service. Strange how hard it is to actually deal with async from the perspective of gracefully bringing down a service and app. My solution was to use mp.Processes and to terminate a process via frontend sync service like flask. Now frontend nodejs can issue a call to Flask endPoint asking to start uvicorn/fastAPI programmatically and use an alternate endPoint to gracefully terminate() same. For me this works horizontally now and I can bring a number of verticals (uvicorn/fastAPI) services up and down on demand ...

Thanks,

On Tue, Sep 13, 2022 at 9:30 PM Caleb Hattingh @.***> wrote:

It depends most strongly on the REST servers you're using. Each of the frameworks (aiohttp, fastapi, sanic, etc.) will have a slightly different way of shutting down. You will have to study the docs of your chosen framework to see how they want you to start up and shut down their server. Usually they (the frameworks) control the starting up and shutting down of the event loop, so aiorun probably can't help much here.

— Reply to this email directly, view it on GitHub https://github.com/cjrh/aiorun/issues/76#issuecomment-1246119013, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEC5FXOX34FI6AEOTEBYQDV6ETD3ANCNFSM6AAAAAAQLYGM3E . You are receiving this because you authored the thread.Message ID: @.***>

-- Aleksandar Kacanski - Sasha

Python Rocks, natural or digital, regardless ...

catchthemonster avatar Sep 14 '22 16:09 catchthemonster

however I want to be able programmatically to stop the loop. anything after run(loop=loop) is not going to work!

To answer this question from the perspective of aiorun, all you have to do is call asyncio.get_running_loop().stop(). This will initiate the shutdown process, which means the sequence of steps listed in the README such as task cancellation, waiting for all tasks to finish, shutting down the executor etc. Whether or not this works for the specific web frameworks you're using is more a matter for those frameworks.

It sounds like you've got a solution using processes and process termination, which I agree is a pretty clean way to do it since you will also have memory space isolation between the different services.

cjrh avatar Sep 24 '22 10:09 cjrh

Yes, I agree.

On Sat, Sep 24, 2022, 6:15 AM Caleb Hattingh @.***> wrote:

however I want to be able programmatically to stop the loop. anything after run(loop=loop) is not going to work!

To answer this question from the perspective of aiorun, all you have to do is call asyncio.get_running_loop().stop(). This will initiate the shutdown process, which means the sequence of steps listed in the README such as task cancellation, waiting for all tasks to finish, shutting down the executor etc. Whether or not this works for the specific web frameworks you're using is more a matter for those frameworks.

It sounds like you've got a solution using processes and process termination, which I agree is a pretty clean way to do it since you will also have memory space isolation between the different services.

— Reply to this email directly, view it on GitHub https://github.com/cjrh/aiorun/issues/76#issuecomment-1256931461, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEC5FRUCLR3FYIF56N25WDV73ICNANCNFSM6AAAAAAQLYGM3E . You are receiving this because you authored the thread.Message ID: @.***>

catchthemonster avatar Sep 25 '22 12:09 catchthemonster