-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tim.reichard
committed
Mar 2, 2021
1 parent
f5e5007
commit 6811a5f
Showing
7 changed files
with
79 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""aioradio utils cache script.""" | ||
|
||
from asyncio import create_task, to_thread, sleep, coroutine | ||
from typing import Any, Dict, List, Tuple | ||
|
||
async def manage_async_tasks(items: List[Tuple[coroutine, str]], concurrency: int) -> Dict[str, Any]: | ||
"""Manages a grouping of async tasks, keeping number of active tasks at | ||
the concurrency level by starting new tasks whenver one completes. | ||
Args: | ||
items (List[Dict[str, Any]]): List of tuples (coroutine, name) | ||
concurrency (int): max concurrency | ||
Returns: | ||
Dict[str, Any]: Dict with task name as the key, task result as the value | ||
""" | ||
|
||
results = {} | ||
arr = [create_task(coro) if name is None else create_task(coro, name=name) for coro, name in items[:concurrency]] | ||
count = len(arr) | ||
num_of_items = len(items) | ||
while len(results) < num_of_items: | ||
await sleep(0.001) | ||
for index, task in enumerate(arr): | ||
if task.done(): | ||
results[task.get_name()] = await task | ||
if count < num_of_items: | ||
coro, name = items[count] | ||
arr[index].append(create_task(coro) if name is None else create_task(coro, name=name)) | ||
count += 1 | ||
|
||
return results | ||
|
||
async def manage_async_to_thread_tasks(func: Any, items: List[Dict[str, Any]], concurrency: int) -> Dict[str, Any]: | ||
"""Manages a grouping of async.to_thread tasks, keeping number of active | ||
tasks at the concurrency level by starting new tasks whenver one completes. | ||
Only use with python3.9+. | ||
Args: | ||
func (Any): Function to run in threads | ||
items (List[Dict[str, Any]]): List of dict with kwargs for each task | ||
concurrency (int): max concurrency | ||
Returns: | ||
Dict[str, Any]: Dict with generi task name as the key, task result as the value | ||
""" | ||
|
||
results = {} | ||
arr = [create_task(to_thread(func, **i)) for i in items[:concurrency]] | ||
count = len(arr) | ||
num_of_items = len(items) | ||
while len(results) < num_of_items: | ||
await sleep(0.001) | ||
for index, task in enumerate(arr): | ||
if task.done(): | ||
results[task.get_name()] = await task | ||
if count < num_of_items: | ||
arr[index] = create_task(to_thread(func, **items[count])) | ||
count += 1 | ||
|
||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters