Advertisement
Guest User

Untitled

a guest
Jun 6th, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. async def connection_handler(...):
  2.     ...
  3.  
  4. async def test(stop_request):
  5.     # occasionally stop the server
  6.     while True:
  7.         await asyncio.sleep(1)
  8.         print('requesting stop')
  9.         stop_request.set()
  10.  
  11. async def main():
  12.     stop_request = asyncio.Event()
  13.     asyncio.create_task(test(stop_request))
  14.     while True:
  15.         print('starting the server')
  16.         server = await websockets.serve(connection_handler, 'localhost', 6789)
  17.         await stop_request.wait()
  18.         stop_request.clear()
  19.         print('stopping the server')
  20.         server.close()
  21.         await server.wait_closed()
  22.  
  23. asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement