aboutsummaryrefslogtreecommitdiff
path: root/webui.py
diff options
context:
space:
mode:
authorAUTOMATIC <16777216c@gmail.com>2023-04-29 10:21:01 +0300
committerAUTOMATIC <16777216c@gmail.com>2023-04-29 10:21:01 +0300
commit86bafb625ae2671722abe1f95a4c0fc5e38381e3 (patch)
tree3752e3eb475d299b6846fab943325f92daa2d3e1 /webui.py
parent24dec9c832140754be84e9f7c5fad79dd714e760 (diff)
put asyncio fix into a function to make it more obvious where it starts and ends
Diffstat (limited to 'webui.py')
-rw-r--r--webui.py67
1 files changed, 34 insertions, 33 deletions
diff --git a/webui.py b/webui.py
index fa8e21ae..0e2a3df0 100644
--- a/webui.py
+++ b/webui.py
@@ -5,7 +5,6 @@ import importlib
import signal
import re
import warnings
-import asyncio
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
@@ -67,45 +66,45 @@ if cmd_opts.server_name:
else:
server_name = "0.0.0.0" if cmd_opts.listen else None
-if sys.platform == "win32" and hasattr(asyncio, "WindowsSelectorEventLoopPolicy"):
- # "Any thread" and "selector" should be orthogonal, but there's not a clean
- # interface for composing policies so pick the right base.
- _BasePolicy = asyncio.WindowsSelectorEventLoopPolicy # type: ignore
-else:
- _BasePolicy = asyncio.DefaultEventLoopPolicy
-
-
-class AnyThreadEventLoopPolicy(_BasePolicy): # type: ignore
- """Event loop policy that allows loop creation on any thread.
- The default `asyncio` event loop policy only automatically creates
- event loops in the main threads. Other threads must create event
- loops explicitly or `asyncio.get_event_loop` (and therefore
- `.IOLoop.current`) will fail. Installing this policy allows event
- loops to be created automatically on any thread, matching the
- behavior of Tornado versions prior to 5.0 (or 5.0 on Python 2).
-
- Usage::
+def fix_asyncio_event_loop_policy():
+ """
+ The default `asyncio` event loop policy only automatically creates
+ event loops in the main threads. Other threads must create event
+ loops explicitly or `asyncio.get_event_loop` (and therefore
+ `.IOLoop.current`) will fail. Installing this policy allows event
+ loops to be created automatically on any thread, matching the
+ behavior of Tornado versions prior to 5.0 (or 5.0 on Python 2).
+ """
- asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
+ import asyncio
- .. versionadded:: 5.0
+ if sys.platform == "win32" and hasattr(asyncio, "WindowsSelectorEventLoopPolicy"):
+ # "Any thread" and "selector" should be orthogonal, but there's not a clean
+ # interface for composing policies so pick the right base.
+ _BasePolicy = asyncio.WindowsSelectorEventLoopPolicy # type: ignore
+ else:
+ _BasePolicy = asyncio.DefaultEventLoopPolicy
- """
+ class AnyThreadEventLoopPolicy(_BasePolicy): # type: ignore
+ """Event loop policy that allows loop creation on any thread.
+ Usage::
- def get_event_loop(self) -> asyncio.AbstractEventLoop:
- try:
- return super().get_event_loop()
- except (RuntimeError, AssertionError):
- # This was an AssertionError in python 3.4.2 (which ships with debian jessie)
- # and changed to a RuntimeError in 3.4.3.
- # "There is no current event loop in thread %r"
- loop = self.new_event_loop()
- self.set_event_loop(loop)
- return loop
+ asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
+ """
+ def get_event_loop(self) -> asyncio.AbstractEventLoop:
+ try:
+ return super().get_event_loop()
+ except (RuntimeError, AssertionError):
+ # This was an AssertionError in python 3.4.2 (which ships with debian jessie)
+ # and changed to a RuntimeError in 3.4.3.
+ # "There is no current event loop in thread %r"
+ loop = self.new_event_loop()
+ self.set_event_loop(loop)
+ return loop
-asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
+ asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
def check_versions():
@@ -140,6 +139,8 @@ Use --skip-version-check commandline argument to disable this check.
def initialize():
+ fix_asyncio_event_loop_policy()
+
check_versions()
extensions.list_extensions()