Skip to content

Commit f2edcf0

Browse files
committed
revert dispatch method override
1 parent 53eee4f commit f2edcf0

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ Using the following categories, list your changes in this order:
4343
- ReactPy Websocket will now decode messages via `orjson` resulting in an ~6% overall performance boost.
4444
- Built-in asyncio event loops are now patched via `nest_asyncio` to be re-enterant, resulting in an ~10% overall performance boost. This has no performance impact if you are running your webserver with `uvloop`.
4545

46-
### Changed
47-
48-
- Changed implementation of `REACTPY_BACKHAUL_THREAD` to attempt increased performance compatibility.
49-
5046
### Fixed
5147

5248
- Fix bug where `REACTPY_WEBSOCKET_URL` always generates a warning if unset.

src/reactpy_django/websocket/consumer.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import asyncio
66
import contextlib
77
import logging
8+
from concurrent.futures import Future
89
from datetime import timedelta
910
from threading import Thread
1011
from typing import Any, MutableMapping, Sequence
@@ -41,7 +42,7 @@ class ReactpyAsyncWebsocketConsumer(AsyncJsonWebsocketConsumer):
4142

4243
async def connect(self) -> None:
4344
"""The browser has connected."""
44-
from reactpy_django.config import REACTPY_AUTH_BACKEND
45+
from reactpy_django.config import REACTPY_AUTH_BACKEND, REACTPY_BACKHAUL_THREAD
4546

4647
await super().connect()
4748

@@ -77,36 +78,33 @@ async def connect(self) -> None:
7778
)
7879

7980
# Start the component dispatcher
80-
self.recv_queue: asyncio.Queue = asyncio.Queue()
81-
self.dispatcher = asyncio.create_task(self.run_dispatcher())
81+
self.dispatcher: Future | asyncio.Task
82+
self.threaded = REACTPY_BACKHAUL_THREAD
83+
if self.threaded:
84+
if not backhaul_thread.is_alive():
85+
await asyncio.to_thread(
86+
_logger.debug, "Starting ReactPy backhaul thread."
87+
)
88+
backhaul_thread.start()
89+
self.dispatcher = asyncio.run_coroutine_threadsafe(
90+
self.run_dispatcher(), backhaul_loop
91+
)
92+
else:
93+
self.dispatcher = asyncio.create_task(self.run_dispatcher())
8294

8395
async def disconnect(self, code: int) -> None:
8496
"""The browser has disconnected."""
8597
self.dispatcher.cancel()
86-
await self.dispatcher
8798
await super().disconnect(code)
8899

89100
async def receive_json(self, content: Any, **_) -> None:
90101
"""Receive a message from the browser. Typically, messages are event signals."""
91-
await self.recv_queue.put(content)
92-
93-
async def dispatch(self, message):
94-
"""Override the Django Channels dispatch method to allow running the ASGI
95-
dispatcher in a thread."""
96-
from reactpy_django.config import REACTPY_BACKHAUL_THREAD
97-
98-
if REACTPY_BACKHAUL_THREAD:
99-
if not backhaul_thread.is_alive():
100-
await asyncio.to_thread(
101-
_logger.debug, "Starting ReactPy backhaul thread."
102-
)
103-
backhaul_thread.start()
104-
105-
return asyncio.run_coroutine_threadsafe(
106-
super().dispatch(message), backhaul_loop
107-
).result()
108-
109-
return await super().dispatch(message)
102+
if self.threaded:
103+
asyncio.run_coroutine_threadsafe(
104+
self.recv_queue.put(content), backhaul_loop
105+
)
106+
else:
107+
await self.recv_queue.put(content)
110108

111109
@classmethod
112110
async def decode_json(cls, text_data):
@@ -128,6 +126,7 @@ async def run_dispatcher(self):
128126
dotted_path = scope["url_route"]["kwargs"]["dotted_path"]
129127
uuid = scope["url_route"]["kwargs"]["uuid"]
130128
search = scope["query_string"].decode()
129+
self.recv_queue: asyncio.Queue = asyncio.Queue()
131130
connection = Connection( # For `use_connection`
132131
scope=scope,
133132
location=Location(

0 commit comments

Comments
 (0)