Skip to content

Commit

Permalink
Merge pull request #2829 from finos/tkp/loops
Browse files Browse the repository at this point in the history
Allow for passthrough of event loop in psp webserver handlers
  • Loading branch information
texodus authored Dec 2, 2024
2 parents a21718a + 601e090 commit e57c810
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
10 changes: 0 additions & 10 deletions examples/python-starlette/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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


Expand Down
3 changes: 2 additions & 1 deletion rust/perspective-python/perspective/handlers/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion rust/perspective-python/perspective/handlers/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions rust/perspective-python/perspective/handlers/tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

0 comments on commit e57c810

Please sign in to comment.