From 601e090a271cf26cf9d921d18b2012fd292a6217 Mon Sep 17 00:00:00 2001 From: Tim Paine <3105306+timkpaine@users.noreply.github.com> Date: Wed, 6 Nov 2024 16:53:39 -0500 Subject: [PATCH] allow for passthrough of event loop in psp webserver handlers Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com> --- examples/python-starlette/server.py | 10 ---------- .../perspective-python/perspective/handlers/aiohttp.py | 3 ++- .../perspective/handlers/starlette.py | 3 ++- .../perspective-python/perspective/handlers/tornado.py | 5 +++-- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/examples/python-starlette/server.py b/examples/python-starlette/server.py index b1bc6f602b..f1d3baa0a0 100644 --- a/examples/python-starlette/server.py +++ b/examples/python-starlette/server.py @@ -18,7 +18,6 @@ import uvicorn from fastapi import FastAPI, WebSocket -from fastapi.middleware.cors import CORSMiddleware from starlette.responses import FileResponse from starlette.staticfiles import StaticFiles @@ -64,21 +63,12 @@ async def websocket_handler(websocket: WebSocket): ) await handler.run() - # static_html_files = StaticFiles(directory="../python-tornado", html=True) static_html_files = StaticFiles(directory="../python-tornado", html=True) app = FastAPI() app.add_api_websocket_route("/websocket", websocket_handler) app.get("/node_modules/{rest_of_path:path}")(static_node_modules_handler) app.mount("/", static_html_files) - - app.add_middleware( - CORSMiddleware, - allow_origins=["*"], - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], - ) return app diff --git a/rust/perspective-python/perspective/handlers/aiohttp.py b/rust/perspective-python/perspective/handlers/aiohttp.py index 421889720d..f4d459a217 100644 --- a/rust/perspective-python/perspective/handlers/aiohttp.py +++ b/rust/perspective-python/perspective/handlers/aiohttp.py @@ -37,11 +37,12 @@ class PerspectiveAIOHTTPHandler(object): def __init__(self, **kwargs): self.server = kwargs.pop("perspective_server", perspective.GLOBAL_SERVER) self._request = kwargs.pop("request") + self._loop = kwargs.pop("loop", asyncio.get_event_loop()) super().__init__(**kwargs) async def run(self) -> web.WebSocketResponse: def inner(msg): - asyncio.get_running_loop().create_task(self._ws.send_bytes(msg)) + self._loop.create_task(self._ws.send_bytes(msg)) self.session = self.server.new_session(inner) try: diff --git a/rust/perspective-python/perspective/handlers/starlette.py b/rust/perspective-python/perspective/handlers/starlette.py index 4a12fd0194..963ba1c3ef 100644 --- a/rust/perspective-python/perspective/handlers/starlette.py +++ b/rust/perspective-python/perspective/handlers/starlette.py @@ -34,11 +34,12 @@ class PerspectiveStarletteHandler(object): def __init__(self, **kwargs): self._server = kwargs.pop("perspective_server", perspective.GLOBAL_SERVER) self._websocket = kwargs.pop("websocket") + self._loop = kwargs.pop("loop", asyncio.get_event_loop()) super().__init__(**kwargs) async def run(self) -> None: def inner(msg): - asyncio.get_running_loop().create_task(self._websocket.send_bytes(msg)) + self._loop.create_task(self._websocket.send_bytes(msg)) self.session = self._server.new_session(inner) diff --git a/rust/perspective-python/perspective/handlers/tornado.py b/rust/perspective-python/perspective/handlers/tornado.py index 4abae77c73..a2adf35f5b 100644 --- a/rust/perspective-python/perspective/handlers/tornado.py +++ b/rust/perspective-python/perspective/handlers/tornado.py @@ -45,8 +45,9 @@ class PerspectiveTornadoHandler(WebSocketHandler): def check_origin(self, origin): return True - def initialize(self, perspective_server=perspective.GLOBAL_SERVER): + def initialize(self, perspective_server=perspective.GLOBAL_SERVER, loop=None): self.server = perspective_server + self.loop = loop or IOLoop.current() def open(self): def inner(msg): @@ -63,4 +64,4 @@ def on_message(self, msg: bytes): return self.session.handle_request(msg) - IOLoop.current().call_later(0, self.session.poll) + self.loop.call_later(0, self.session.poll)