Skip to content

Commit

Permalink
Removed the kwargs parameter
Browse files Browse the repository at this point in the history
We can add it back later, just needs to be consistent across the worker thread/interpreter/process APIs
  • Loading branch information
agronholm committed Jan 4, 2025
1 parent 667b157 commit 65ba5e1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
18 changes: 8 additions & 10 deletions src/anyio/to_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pickle
import sys
from collections import deque
from collections.abc import Callable, Mapping
from collections.abc import Callable
from textwrap import dedent
from typing import Any, Final, TypeVar

Expand Down Expand Up @@ -43,8 +43,8 @@ class Worker:
item = queues.get(queue_id)[0]
try:
func, args, kwargs = loads(item)
retval = func(*args, **kwargs)
func, args = loads(item)
retval = func(*args)
except BaseException as exc:
is_exception = True
retval = exc
Expand Down Expand Up @@ -93,15 +93,17 @@ def destroy(self) -> None:
queues.destroy(self._queue_id)

def _call(
self, func: Callable[..., T_Retval], args: tuple[Any], kwargs: dict[str, Any]
self,
func: Callable[..., T_Retval],
args: tuple[Any],
) -> tuple[Any, bool]:
import _interpqueues as queues
import _interpreters as interpreters

if not self._initialized:
self.initialize()

payload = pickle.dumps((func, args, kwargs), pickle.HIGHEST_PROTOCOL)
payload = pickle.dumps((func, args), pickle.HIGHEST_PROTOCOL)
queues.put(self._queue_id, payload, FMT_PICKLED, UNBOUND) # type: ignore[call-arg]

res: Any
Expand All @@ -119,14 +121,12 @@ async def call(
self,
func: Callable[..., T_Retval],
args: tuple[Any],
kwargs: dict[str, Any],
limiter: CapacityLimiter,
) -> T_Retval:
result, is_exception = await to_thread.run_sync(
self._call,
func,
args,
kwargs,
limiter=limiter,
)
if is_exception:
Expand All @@ -145,7 +145,6 @@ def _stop_workers(workers: deque[Worker]) -> None:
async def run_sync(
func: Callable[[Unpack[PosArgsT]], T_Retval],
*args: Unpack[PosArgsT],
kwargs: Mapping[str, Any] | None = None,
limiter: CapacityLimiter | None = None,
) -> T_Retval:
"""
Expand All @@ -161,7 +160,6 @@ async def run_sync(
:param func: a callable
:param args: positional arguments for the callable
:param kwargs: keyword arguments for the callable
:param limiter: capacity limiter to use to limit the total amount of subinterpreters
running (if omitted, the default limiter is used)
:return: the result of the call
Expand All @@ -188,7 +186,7 @@ async def run_sync(
worker = Worker()

try:
return await worker.call(func, args, kwargs or {}, limiter)
return await worker.call(func, args, limiter)
finally:
# Prune workers that have been idle for too long
now = current_time()
Expand Down
3 changes: 2 additions & 1 deletion tests/test_to_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
from collections.abc import AsyncGenerator
from functools import partial

import pytest
from pytest import fixture
Expand Down Expand Up @@ -41,7 +42,7 @@ async def test_run_sync() -> None:

async def test_args_kwargs() -> None:
"""Test that partial() can be used to pass keyword arguments."""
result = await to_interpreter.run_sync(sorted, ["a", "b"], kwargs={"reverse": True})
result = await to_interpreter.run_sync(partial(sorted, reverse=True), ["a", "b"])
assert result == ["b", "a"]


Expand Down

0 comments on commit 65ba5e1

Please sign in to comment.