Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Feb 13, 2024
1 parent 71ee20b commit 1343e67
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions a_sync/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def __init__(self, coro_fn: Callable[Concatenate[K, P], Awaitable[V]] = None, *i
self._coro_fn = coro_fn
self._coro_fn_kwargs = coro_fn_kwargs
self._name = name
self._iterables = iterables
if iterables:
self._loader = create_task(exhaust_iterator(self._tasks_for_iterables(*iterables)))
else:
self._loader = None
def __setitem__(self, item: Any, value: Any) -> None:
raise NotImplementedError("You cannot manually set items in a TaskMapping")
def __getitem__(self, item: K) -> "asyncio.Task[V]":
Expand All @@ -38,7 +41,7 @@ def __getitem__(self, item: K) -> "asyncio.Task[V]":
super().__setitem__(item, task)
return task
async def map(self, *iterables: AnyIterable[K], pop: bool = True, yields: Literal['keys', 'both'] = 'both') -> AsyncIterator[Tuple[K, V]]:
if self or self._iterables:
if self:
raise exceptions.MappingNotEmptyError
async for _ in self._tasks_for_iterables(*iterables):
async for key, value in self.yield_completed(pop=pop):
Expand All @@ -51,15 +54,11 @@ async def yield_completed(self, pop: bool = True) -> AsyncIterator[Tuple[K, V]]:
if pop:
task = self.pop(k)
yield k, await task
async def _load_iterables(self, *iterables) -> None:
if self._iterables and not self:
await exhaust_iterator(self._tasks_for_iterables(*iterables))
async def _tasks_for_iterables(self, *iterables) -> AsyncIterator["asyncio.Task[V]"]:
async for key in as_yielded(*[_yield_keys(iterable) for iterable in iterables]): # type: ignore [attr-defined]
yield self[key] # ensure task is running



__persisted_tasks: Set["asyncio.Task[Any]"] = set()

async def __await(awaitable: Awaitable[T]) -> T:
Expand Down

0 comments on commit 1343e67

Please sign in to comment.