-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
43 lines (32 loc) · 1.13 KB
/
main.py
File metadata and controls
43 lines (32 loc) · 1.13 KB
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
import asyncio
import logging
import os
from signal import SIGINT, SIGTERM
from app.pubsub import start_redis_subscription
from app.websockets import broadcast, start_websocket_server
logging.basicConfig(
format="%(asctime)s %(message)s",
level=logging.INFO,
)
host = os.getenv("WEBSOCKET_SERVER_HOST", "localhost")
port = int(os.getenv("WEBSOCKET_SERVER_PORT", 8765))
redis_url = os.getenv("WEBSOCKET_SERVER_REDIS_URL", "redis://redis")
def send_message_to_all(message: str):
broadcast(message)
async def main():
async with asyncio.TaskGroup() as tg:
tg.create_task(start_websocket_server(host, port))
tg.create_task(start_redis_subscription(redis_url, send_message_to_all))
if __name__ == "__main__":
logging.info("🎸 Websocket server started.")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
main_task = asyncio.ensure_future(main())
for signal in [SIGINT, SIGTERM]:
loop.add_signal_handler(signal, main_task.cancel)
try:
loop.run_until_complete(main_task)
except asyncio.exceptions.CancelledError:
pass
finally:
loop.close()