forked from vi2k6/Music-Player
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Vɪᴠᴇᴋ
authored
May 5, 2021
1 parent
b17e96a
commit 13d3d5e
Showing
2 changed files
with
42 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .queues import put, get, is_empty, task_done, clear |
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 |
---|---|---|
@@ -1 +1,42 @@ | ||
from asyncio import Queue, QueueEmpty as Empty | ||
from typing import Dict, Union | ||
|
||
queues: Dict[int, Queue] = {} | ||
|
||
|
||
async def put(chat_id: int, **kwargs) -> int: | ||
if chat_id not in queues: | ||
queues[chat_id] = Queue() | ||
await queues[chat_id].put({**kwargs}) | ||
return queues[chat_id].qsize() | ||
|
||
|
||
def get(chat_id: int) -> Union[Dict[str, str], None]: | ||
if chat_id in queues: | ||
try: | ||
return queues[chat_id].get_nowait() | ||
except Empty: | ||
return None | ||
|
||
|
||
def is_empty(chat_id: int) -> bool: | ||
if chat_id in queues: | ||
return queues[chat_id].empty() | ||
return True | ||
|
||
|
||
def task_done(chat_id: int): | ||
if chat_id in queues: | ||
try: | ||
queues[chat_id].task_done() | ||
except ValueError: | ||
pass | ||
|
||
|
||
def clear(chat_id: int): | ||
if chat_id in queues: | ||
if queues[chat_id].empty(): | ||
raise Empty | ||
else: | ||
queues[chat_id].queue = [] | ||
raise Empty |