MCPcopy Index your code
hub / github.com/pywebio/PyWebIO / wait_to_stop_loop

Function wait_to_stop_loop

pywebio/platform/tornado.py:360–392  ·  view source on GitHub ↗

当只剩当前线程和Daemon线程运行时,关闭Server When only the current thread and Daemon thread are running, close the Server

(server)

Source from the content-addressed store, hash-verified

358 logger.debug('ScriptModeSession closed')
359
360 async def wait_to_stop_loop(server):
361 """当只剩当前线程和Daemon线程运行时,关闭Server
362 When only the current thread and Daemon thread are running, close the Server"""
363
364 # 包括当前线程在内的非Daemon线程数
365 # The number of non-Daemon threads(including the current thread)
366 alive_none_daemonic_thread_cnt = None
367 while alive_none_daemonic_thread_cnt != 1:
368 alive_none_daemonic_thread_cnt = sum(
369 1 for t in threading.enumerate() if t.is_alive() and not t.isDaemon()
370 )
371 await asyncio.sleep(0.5)
372
373 if SingleSessionWSHandler.session and SingleSessionWSHandler.session.need_keep_alive():
374 while not SingleSessionWSHandler.instance.closed:
375 await asyncio.sleep(0.5)
376
377 # 关闭Websocket连接
378 # Close the Websocket connection
379 if SingleSessionWSHandler.instance:
380 SingleSessionWSHandler.instance.close()
381
382 server.stop()
383 logger.debug('Closing tornado ioloop...')
384 tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task() and not t.done()]
385 for task in tasks:
386 task.cancel()
387
388 # 必须需要 await asyncio.sleep ,否则上方 task.cancel() 调用无法调度生效
389 # This line must be required, otherwise the `task.cancel()` call cannot be scheduled to take effect
390 await asyncio.sleep(0)
391
392 tornado.ioloop.IOLoop.current().stop()
393
394 def server_thread():
395 from tornado.log import access_log, app_log, gen_log

Callers

nothing calls this directly

Calls 3

stopMethod · 0.80
need_keep_aliveMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…