vibora
vibora copied to clipboard
How to gracefully shut down vibora server?
When I try ctrl+C, I get this:
# Vibora (0.0.7) # http://127.0.0.1:5000
^C^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/multiprocessing/popen_fork.py", line 28, in poll
pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
What's the proper way to shut down vibora?
@frnkvieira it would be nice to have an input. When i press q
it should stop. When i press r
it should reload. What do you think about this idea?
@danieldaeschle Great Idea.
I change the file https://github.com/vibora-io/vibora/blob/master/vibora/workers/handler.py#L87 and add SIGINT signal at line 88th.
try:
loop.add_signal_handler(signal.SIGTERM, handle_kill_signal)
loop.add_signal_handler(signal.SIGINT , handle_kill_signal) # listening SIGINT signal like Ctrl+C
loop.run_forever()
except (SystemExit, KeyboardInterrupt):
loop.stop()
Add the BEFORE_SERVER_START handler to server code
import asyncio
from vibora import Vibora, Request
from vibora.responses import StreamingResponse
from vibora.hooks import Events
from socket import SOL_SOCKET, SO_LINGER
app = Vibora()
@app.route('/')
async def home(request: Request):
async def slow_streaming():
for _ in range(0,3):
yield b'123'
await asyncio.sleep(1)
return StreamingResponse(slow_streaming)
# add BEFORE_SERVER_START handler for add linger option to all socket instance
@app.handle(Events.BEFORE_SERVER_START)
def before_server_stop(app: Vibora):
for w in app.workers:
if w.socket != None:
w.socket.setsockopt(SOL_SOCKET, SO_LINGER, pack('ii', 1, 0)) # enable socket linger option
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8000)
Now, I don't worry that the clients don't receive full responses after the server shutdown on server had received client requests. ^^