Sanic: app.stop() does not stop the server

Created on 4 Nov 2018  路  7Comments  路  Source: sanic-org/sanic

Describe the bug
I am trying to stop the server with app.stop() but it seems that it does not work. The server still continues running. I am running the server in a seperate thread and calling app.stop() from the main thread.

Code snippet

from threading import Thread
from sanic import Sanic

app = Sanic()
thread = Thread(target=app.run, name="sanicAPI", kwargs=dict(host="0.0.0.0", port=8080, debug=True))
thread.start()

app.stop()
thread.join()

Expected behavior
The server should shut down and the thread should join back into the main thread.

Environment:

  • OS: Windows 10 (1803), build 17134.345
  • Sanic Version: 0.8.3

Additional context
I am starting the server in another thread because I have an application where I additionally need a very small API server to handle tiny amounts of requests to control the application or receive data from it.

Am I doing something wrong? Any kind of help is appreciated. Thanks for the great project.

documentation stale

Most helpful comment

@d-Rickyy-b I thought it had been in the docs, because that is basically the setup that I am using. But, now I am looking back and forth thru them and cannot find it. I must have used it from the examples. I'll tag this as something worth documenting.

All 7 comments

@d-Rickyy-b did you take a look at the Run Async example provided in the example section?

@harshanarayana Thank you very much. I actually did not find it yet. It looks promising.

I will try to implement that tonight and will report back if that solves my issue :).

Maybe that can be referenced in the documentation? (If it already is, I must be blind, sorry)

@d-Rickyy-b I thought it had been in the docs, because that is basically the setup that I am using. But, now I am looking back and forth thru them and cannot find it. I must have used it from the examples. I'll tag this as something worth documenting.

app.stop() just stops the running event_loop. and each thread has its own event loop. Also you probably won't need multiple threads for async application.

@yunstanford I just wanted the API to run in another than the main thread so that it's not blocking the main thread. The posted example helped a lot. I am not finished yet but I guess I can solve the initial issue I got with it.

@d-Rickyy-b take a look at this. threading and asyncio have some requirements to run properly.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings