Skip to content

Commit

Permalink
small fixes and codestyle polish
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels authored and iisakkirotko committed Feb 5, 2024
1 parent 662b3b6 commit c96bd2c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
6 changes: 3 additions & 3 deletions solara/server/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@ async def process_messages_task(self):
await self.ws.send_text(first)

def close(self):
self.portal.call(self.ws.close)
self.portal.call(self.ws.close) # type: ignore

def send_text(self, data: str) -> None:
if settings.main.experimental_performance:
self.to_send.append(data)
else:
self.portal.call(self.ws.send_bytes, data)
self.portal.call(self.ws.send_bytes, data) # type: ignore

def send_bytes(self, data: bytes) -> None:
if settings.main.experimental_performance:
self.to_send.append(data)
else:
self.portal.call(self.ws.send_bytes, data)
self.portal.call(self.ws.send_bytes, data) # type: ignore

async def receive(self):
if hasattr(self.portal, "start_task_soon"):
Expand Down
25 changes: 9 additions & 16 deletions solara/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,16 @@
class Task(Generic[P, R], abc.ABC):
def __init__(self):
self.result = solara.reactive(
# cast(Optional[solara.Result[R]], None)
solara.Result[R](
value=None,
state=solara.ResultState.INITIAL,
)
)
# self.thread_pool = ThreadPoolExecutor(max_workers=1)
self.last_value: Optional[R] = None

def reset(self):
self.running_thread = None
self.cancel()
# self.result.value = None

@property
def value(self) -> Optional[R]:
Expand Down Expand Up @@ -88,13 +85,9 @@ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> None:

def retry():
self.__call__(*args, **kwargs)
# nonlocal task_for_this_call
# task_for_this_call.cancel()
# self.current_task = task_for_this_call = asyncio.create_task(runner())

def cancel():
assert task_for_this_call is not None
# assert asyncio.current_task() != task_for_this_call
if asyncio.current_task() == task_for_this_call:
raise asyncio.CancelledError()
else:
Expand Down Expand Up @@ -153,7 +146,7 @@ def cancel(self) -> None:

def __call__(self, *args: P.args, **kwargs: P.kwargs) -> None:
self._last_finished_event = _last_finished_event = threading.Event()
self.current_thread = threading.Thread(target=lambda: self._run(_last_finished_event, *args, **kwargs), daemon=True)
self.current_thread = current_thread = threading.Thread(target=lambda: self._run(_last_finished_event, *args, **kwargs), daemon=True)

def retry():
self._run(_last_finished_event, *args, **kwargs)
Expand All @@ -163,7 +156,7 @@ def retry():
"_retry": retry,
}
self.result.value = solara.Result[R](value=self.last_value, state=solara.ResultState.STARTING, **common)
self.current_thread.start()
current_thread.start()

def _run(self, _last_finished_event, *args: P.args, **kwargs: P.kwargs) -> None:
def retry():
Expand Down Expand Up @@ -258,21 +251,21 @@ class _CommonArgs(typing_extensions.TypedDict):


# TODO: Not sure if we want to use this, or have all local variables in Task subclasses be reactive vars
class TaskProxy:
class Proxy:
def __init__(self, factory):
self.task = Singleton(factory)
self._instance = Singleton(factory)

def __getattr__(self, name):
return getattr(self.task.value, name)
return getattr(self._instance.value, name)

def __setattr__(self, name, value):
if name == "task":
if name == "_instance":
super().__setattr__(name, value)
else:
setattr(self.task.value, name, value)
setattr(self._instance.value, name, value)

def __call__(self, *args, **kwargs):
return self.task.value(*args, **kwargs)
return self._instance.value(*args, **kwargs)


# TODO: coroutine typing
Expand Down Expand Up @@ -333,7 +326,7 @@ def create_task():
else:
return TaskThreaded[P, R](cast(Callable[P, R], function))

return cast(Task[P, R], TaskProxy(create_task))
return cast(Task[P, R], Proxy(create_task))

if function is None:
return wrapper
Expand Down

0 comments on commit c96bd2c

Please sign in to comment.