Skip to content

Commit

Permalink
ActorCallerThreaded do not store clients other than thread-local
Browse files Browse the repository at this point in the history
  • Loading branch information
frostyplanet committed Nov 25, 2024
1 parent f5f4676 commit f0fe64c
Showing 1 changed file with 2 additions and 19 deletions.
21 changes: 2 additions & 19 deletions python/xoscar/backends/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,14 @@ class ActorCallerThreaded:
"""

def __init__(self):
self.lock = threading.Lock()
self.local = threading.local()
self.all_callers = []

def _get_local(self) -> ActorCaller:
try:
return self.local.caller
except AttributeError:
caller = ActorCaller()
self.local.caller = caller
with self.lock:
self.all_callers.append(caller)
return caller

async def get_copy_to_client(self, address: str) -> CallerClient:
Expand Down Expand Up @@ -96,25 +92,12 @@ async def call(
return await caller.call(router, dest_address, message, wait)

async def stop(self):
with self.lock:
all_callers = self.all_callers
local_caller = self._get_local()
for caller in all_callers:
if caller == local_caller:
await caller.stop()
else:
future = asyncio.run_coroutine_threadsafe(caller.stop(), caller._loop)
await future.result()
await local_caller.stop()

def stop_nonblock(self):
with self.lock:
all_callers = self.all_callers
local_caller = self._get_local()
for caller in all_callers:
if caller == local_caller:
caller.stop_nonblock()
else:
caller._loop.call_soon_threadsafe(caller.stop_nonblock)
local_caller.stop_nonblock()


class CallerClient:
Expand Down

0 comments on commit f0fe64c

Please sign in to comment.