Форум сайта python.su
Всем привет! Имеется такой код:
from aiohttp import web import asyncio async def send_msg1(app): while True: for ws in app['admin_sockets']: await ws.send_str('msg1') await asyncio.sleep(10) async def send_msg2(app): while True: for ws in app['admin_sockets']: await ws.send_str('msg2') await asyncio.sleep(20) async def ws_handler(request): ws = web.WebSocketResponse() await ws.prepare(request) is_admin = request.path.startswith('/ws/admin') a_socks = request.app['admin_sockets'] u_socks = request.app['user_sockets'] if is_admin: a_socks.append(ws) else: u_socks.append(ws) try: async for msg in ws: await ws.send_str(msg.data) except Exception as e: pass finally: if is_admin: a_socks.remove(ws) else: u_socks.remove(ws) return ws app = web.Application() app.router.add_get('/ws', ws_handler) app.router.add_get('/ws/admin', ws_handler) app['admin_sockets'] = [] app['user_sockets'] = [] loop = asyncio.get_event_loop() loop.create_task(send_msg1(app)) loop.create_task(send_msg2(app))
Отредактировано a-lexx (Фев. 6, 2018 08:19:13)
Офлайн