-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
54 lines (40 loc) · 1.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import asyncio
import logging
import uvicorn
from app.api import app as app_fastapi
class Server(uvicorn.Server):
"""Customized uvicorn.Server
Uvicorn server overrides signals and we need to include
Rocketry to the signals."""
def handle_exit(self, sig: int, frame) -> None:
"""Handle exit signal"""
print("@@@@@@@@@ Exit signal received @@@@@@@@@")
return super().handle_exit(sig, frame)
async def main():
server = Server(
config=uvicorn.Config(
app_fastapi,
workers=3,
loop="asyncio",
use_colors=True,
host="0.0.0.0",
access_log=True,
)
)
api = asyncio.create_task(
server.serve()
) # esse pode ser duplicado com outro async task
try:
await asyncio.wait([api]) # aqui se adiciona outro async task
except asyncio.CancelledError:
api.cancel() # Cancel the server task if a CancelledError is caught
await api # Wait for the server task to be cancelled
if __name__ == "__main__":
# Print Rocketry's logs to terminal
logger = logging.getLogger("rocketry.task")
logger.addHandler(logging.StreamHandler())
# Run both applications
try:
asyncio.run(main())
except KeyboardInterrupt:
print("KeyboardInterrupt caught, shutting down gracefully")