Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1f5ed0d
Running ReactPy in a backhaul thread
Archmonger Jul 11, 2023
026676e
less LOCs for backhaul thread
Archmonger Jul 11, 2023
ecb2a0d
simplier effect
Archmonger Jul 11, 2023
f77c5e8
remove excess div
Archmonger Jul 11, 2023
52be745
move RPS endpoint
Archmonger Jul 13, 2023
8e8cb72
add whitenoise
Archmonger Jul 13, 2023
5c01a84
Allow for testing multiple renderers
Archmonger Jul 13, 2023
712c1ea
set debug
Archmonger Jul 13, 2023
a60bb64
sort
Archmonger Jul 13, 2023
64175c4
add component count
Archmonger Jul 13, 2023
e71b402
Add TTL
Archmonger Jul 13, 2023
f997001
add ttl tests
Archmonger Jul 13, 2023
8c3a354
receive queue does not need to be threaded
Archmonger Jul 13, 2023
868c26e
rename loop function
Archmonger Jul 13, 2023
6ab1742
Revert "receive queue does not need to be threaded"
Archmonger Jul 13, 2023
e65f86a
ensure todo list entries are unique
Archmonger Jul 13, 2023
c54c216
events per second tests
Archmonger Jul 14, 2023
5fd014f
use text input instead
Archmonger Jul 14, 2023
8187df7
rename EPS
Archmonger Jul 14, 2023
f434e4c
net io TTL
Archmonger Jul 14, 2023
d8e3b0b
mixed TTL
Archmonger Jul 14, 2023
8866f3f
Add config setting for backhaul thread
Archmonger Jul 14, 2023
12e7ea0
fix formatting
Archmonger Jul 14, 2023
ac03724
add changelog
Archmonger Jul 14, 2023
517caf9
add backhaul to dictionary
Archmonger Jul 14, 2023
ddd718f
Remove excess exceptions, potential attack vector
Archmonger Jul 14, 2023
11b93b4
add logging for daphne issues
Archmonger Jul 14, 2023
690fbda
eps -> erps
Archmonger Jul 15, 2023
bdbe9f0
event timing
Archmonger Jul 15, 2023
b6cb92f
better daphne check
Archmonger Jul 15, 2023
91f90d4
I really hate Ruff
Archmonger Jul 15, 2023
b8c1a9a
change comment
Archmonger Jul 16, 2023
993fe45
better debug check
Archmonger Jul 16, 2023
7b928b4
make run dispatcher public
Archmonger Jul 19, 2023
143c2cd
cleanup
Archmonger Jul 20, 2023
92e9eed
threading for all logging
Archmonger Jul 22, 2023
2f2135f
switch to ruff
Archmonger Jul 22, 2023
6b77479
change line length
Archmonger Jul 22, 2023
8d19322
bump minimum python version
Archmonger Jul 22, 2023
356ed88
use ruff for docs
Archmonger Jul 22, 2023
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add config setting for backhaul thread
  • Loading branch information
Archmonger committed Jul 14, 2023
commit 8866f3fc9457eb2433e9d25a53b442084c2f8286
5 changes: 5 additions & 0 deletions src/reactpy_django/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@
"REACTPY_AUTH_BACKEND",
None,
)
REACTPY_BACKHAUL_THREAD: bool = getattr(
settings,
"REACTPY_BACKHAUL_THREAD",
True,
)
42 changes: 28 additions & 14 deletions src/reactpy_django/websocket/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from reactpy_django.types import ComponentParamData, ComponentWebsocket
from reactpy_django.utils import db_cleanup, func_has_args


_logger = logging.getLogger(__name__)
backhaul_loop = asyncio.new_event_loop()

Expand All @@ -31,19 +30,19 @@ def start_backhaul_loop() -> None:
backhaul_loop.run_forever()


Thread(target=start_backhaul_loop, daemon=True).start()
backhaul_thread = Thread(target=start_backhaul_loop, daemon=True)


class ReactpyAsyncWebsocketConsumer(AsyncJsonWebsocketConsumer):
"""Communicates with the browser to perform actions on-demand."""

async def connect(self) -> None:
"""The browser has connected."""
from reactpy_django.config import REACTPY_AUTH_BACKEND, REACTPY_BACKHAUL_THREAD

await super().connect()

# Authenticate the user, if possible
from reactpy_django.config import REACTPY_AUTH_BACKEND

user: Any = self.scope.get("user")
if user and user.is_authenticated:
try:
Expand All @@ -68,23 +67,38 @@ async def connect(self) -> None:
"Sessions will not be accessible within `use_scope` or `use_websocket`!"
)

# Start allowing component renders
self._reactpy_dispatcher_future = asyncio.run_coroutine_threadsafe(
self._run_dispatch_loop(), loop=backhaul_loop
)
# Start the component dispatcher
self.threaded = REACTPY_BACKHAUL_THREAD
if self.threaded:
if not backhaul_thread.is_alive():
_logger.debug("Starting ReactPy backhaul thread.")
backhaul_thread.start()
self._reactpy_dispatcher_future = asyncio.run_coroutine_threadsafe(
self._run_dispatcher(), loop=backhaul_loop
)
else:
self._reactpy_dispatcher_task = asyncio.create_task(self._run_dispatcher())

async def disconnect(self, code: int) -> None:
"""The browser has disconnected."""
self._reactpy_dispatcher_future.cancel()
if self.threaded:
self._reactpy_dispatcher_future.cancel()
elif self._reactpy_dispatcher_task.done():
await self._reactpy_dispatcher_task
else:
self._reactpy_dispatcher_task.cancel()
await super().disconnect(code)

async def receive_json(self, content: Any, **_) -> None:
"""Receive a message from the browser. Typically messages are event signals."""
asyncio.run_coroutine_threadsafe(
self._reactpy_recv_queue.put(content), loop=backhaul_loop
)
"""Receive a message from the browser. Typically, messages are event signals."""
if self.threaded:
asyncio.run_coroutine_threadsafe(
self._reactpy_recv_queue.put(content), loop=backhaul_loop
)
else:
await self._reactpy_recv_queue.put(content)

async def _run_dispatch_loop(self):
async def _run_dispatcher(self):
"""Runs the main loop that performs component rendering tasks."""
from reactpy_django import models
from reactpy_django.config import (
Expand Down
1 change: 1 addition & 0 deletions tests/test_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,4 @@

# ReactPy Django Settings
REACTPY_AUTH_BACKEND = "django.contrib.auth.backends.ModelBackend"
REACTPY_BACKHAUL_THREAD = "test" not in sys.argv