Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: if clause in apply_async_memory_cache #333

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion a_sync/a_sync/modifiers/cache/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ def apply_async_memory_cache(

# Validate
elif coro_fn is None:
if not (maxsize is None or isinstance(maxsize, int)):
if maxsize is not None and (not isinstance(maxsize, int) or maxsize <= 0):
raise TypeError(
"'lru_cache_maxsize' must be a positive integer or None.", maxsize
)

elif not asyncio.iscoroutinefunction(coro_fn):
raise exceptions.FunctionNotAsync(coro_fn)

Expand Down
33 changes: 21 additions & 12 deletions a_sync/primitives/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class Queue(_Queue[T]):
"""
A generic asynchronous queue that extends the functionality of asyncio.Queue.

This implementation supports retrieving multiple items at once and handling
task processing in both FIFO and LIFO order. It provides enhanced type hinting
This implementation supports retrieving multiple items at once and handling
task processing in both FIFO and LIFO order. It provides enhanced type hinting
support and additional methods for bulk operations.

Inherits from:
Expand Down Expand Up @@ -84,7 +84,7 @@ def get_nowait(self) -> T:

Raises:
:exc:`~asyncio.QueueEmpty`: If the queue is empty.

Returns:
T: The next item in the queue.

Expand Down Expand Up @@ -117,7 +117,7 @@ def put_nowait(self, item: T) -> None:

Args:
item: The item to add to the queue.

Raises:
:exc:`~asyncio.QueueFull`: If the queue is full.

Expand All @@ -130,7 +130,7 @@ async def get_all(self) -> List[T]:
"""
Asynchronously retrieves and removes all available items from the queue.

If the queue is empty, this method will wait until at least one item
If the queue is empty, this method will wait until at least one item
is available before returning.

Returns:
Expand Down Expand Up @@ -237,7 +237,7 @@ class ProcessingQueue(_Queue[Tuple[P, "asyncio.Future[V]"]], Generic[P, V]):
"""
A queue designed for processing tasks asynchronously with multiple workers.

Each item in the queue is processed by a worker, and tasks can return results
Each item in the queue is processed by a worker, and tasks can return results
via asynchronous futures. This queue is ideal for scenarios where tasks need
to be processed concurrently with a fixed number of workers.

Expand Down Expand Up @@ -614,7 +614,10 @@ class PriorityProcessingQueue(_PriorityQueueMixin[T], ProcessingQueue[T, V]):
A priority-based processing queue where tasks are processed based on priority.
# NOTE: WIP
"""
async def put(self, priority: Any, *args: P.args, **kwargs: P.kwargs) -> "asyncio.Future[V]":

async def put(
self, priority: Any, *args: P.args, **kwargs: P.kwargs
) -> "asyncio.Future[V]":
"""
Asynchronously adds a task with priority to the queue.

Expand All @@ -635,7 +638,9 @@ async def put(self, priority: Any, *args: P.args, **kwargs: P.kwargs) -> "asynci
await super().put(self, (priority, args, kwargs, fut))
return fut

def put_nowait(self, priority: Any, *args: P.args, **kwargs: P.kwargs) -> "asyncio.Future[V]":
def put_nowait(
self, priority: Any, *args: P.args, **kwargs: P.kwargs
) -> "asyncio.Future[V]":
"""
Immediately adds a task with priority to the queue without waiting.

Expand Down Expand Up @@ -711,18 +716,22 @@ def _get_key(self, *args, **kwargs) -> _smart._Key:
>>> print(key)
"""
return (args, tuple((kwarg, kwargs[kwarg]) for kwarg in sorted(kwargs)))



class VariablePriorityQueue(_VariablePriorityQueueMixin[T], asyncio.PriorityQueue):
"""
A :class:`~asyncio.PriorityQueue` subclass that allows priorities to be updated (or computed) on the fly.
# NOTE: WIP
"""

class SmartProcessingQueue(_VariablePriorityQueueMixin[T], ProcessingQueue[Concatenate[T, P], V]):

class SmartProcessingQueue(
_VariablePriorityQueueMixin[T], ProcessingQueue[Concatenate[T, P], V]
):
"""
A PriorityProcessingQueue subclass that will execute jobs with the most waiters first
"""

_no_futs = False
"""Whether smart futures are used."""

Expand Down Expand Up @@ -868,4 +877,4 @@ async def __worker_coro(self) -> NoReturn:
except Exception as e:
logger.error("%s for %s is broken!!!", type(self).__name__, self.func)
logger.exception(e)
raise
raise