-
Notifications
You must be signed in to change notification settings - Fork 16
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
feat: add queue support #50
Open
imnotjames
wants to merge
8
commits into
adafruit:main
Choose a base branch
from
imnotjames:support-queue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
99b2808
feat: add queue support
imnotjames 2b70811
chore: add docstring and use `[]` rather than `list()`
imnotjames 814a6c0
chore: add module docstring
imnotjames 1e93f4f
chore: run black
imnotjames 40c8f64
chore: add license to queue
imnotjames 588fe6d
chore: refactor to back by TaskQueue instead of Event
imnotjames 42db9e6
chore: disable pylint warnings for protected access
imnotjames f94cfb3
docs: add queue to the api docs
imnotjames File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
# SPDX-FileCopyrightText: 2019-2020 Damien P. George | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# MicroPython uasyncio module | ||
# MIT license; Copyright (c) 2019-2020 Damien P. George | ||
|
||
# The rest of the library assumes that `_never` and `_task_queue` should be imported from | ||
# core, which angers pylint. | ||
# pylint: disable=protected-access | ||
|
||
""" | ||
Exceptions and classes related to asyncio Queue implementations. | ||
""" | ||
|
||
from . import core | ||
|
||
|
||
class QueueEmpty(Exception): | ||
"""Raised when Queue.get_nowait() is called on an empty Queue.""" | ||
|
||
|
||
class QueueFull(Exception): | ||
"""Raised when the Queue.put_nowait() method is called on a full Queue.""" | ||
|
||
|
||
async def _wait_on_task_queue(task_queue: core.TaskQueue): | ||
task_queue.push(core.cur_task) | ||
# Set calling task's data to the TaskQueue so it can be removed if needed | ||
core.cur_task.data = task_queue | ||
# Send control back | ||
await core._never() | ||
|
||
|
||
def _release_task_queue(task_queue: core.TaskQueue): | ||
while task_queue.peek(): | ||
core._task_queue.push(task_queue.pop()) | ||
|
||
|
||
class Queue: | ||
""" | ||
A queue, useful for coordinating producer and consumer coroutines. | ||
|
||
If maxsize is less than or equal to zero, the queue size is infinite. If it | ||
is an integer greater than 0, then "await put()" will block when the | ||
queue reaches maxsize, until an item is removed by get(). | ||
|
||
Unlike CPython's asyncio.Queue, this implementation is backed by a list rather | ||
than `collections.deque` because smaller boards may not have the library | ||
implemented. | ||
""" | ||
|
||
def __init__(self, maxsize=0): | ||
self.maxsize = maxsize | ||
|
||
self._queue = [] | ||
|
||
self._active_tasks = 0 | ||
|
||
self._waiting_for_completion = core.TaskQueue() | ||
self._waiting_for_put = core.TaskQueue() | ||
self._waiting_for_get = core.TaskQueue() | ||
|
||
def _get(self): | ||
value = self._queue.pop(0) | ||
_release_task_queue(self._waiting_for_get) | ||
return value | ||
|
||
def _put(self, val): | ||
self._queue.append(val) | ||
self._active_tasks += 1 | ||
_release_task_queue(self._waiting_for_put) | ||
|
||
async def get(self): | ||
""" | ||
Remove and return an item from the queue. | ||
|
||
If queue is empty, wait until an item is available. | ||
""" | ||
while self.empty(): | ||
await _wait_on_task_queue(self._waiting_for_put) | ||
return self._get() | ||
|
||
def get_nowait(self): | ||
""" | ||
Remove and return an item from the queue. | ||
|
||
If queue is empty, raise QueueEmpty. | ||
""" | ||
if self.empty(): | ||
raise QueueEmpty() | ||
return self._get() | ||
|
||
async def put(self, val): | ||
""" | ||
Put an item into the queue. | ||
|
||
If the queue is full, waits until a free | ||
slot is available before adding item. | ||
""" | ||
while self.full(): | ||
await _wait_on_task_queue(self._waiting_for_get) | ||
self._put(val) | ||
|
||
def put_nowait(self, val): | ||
""" | ||
Put an item into the queue. | ||
|
||
If the queue is full, raises QueueFull. | ||
""" | ||
if self.full(): | ||
raise QueueFull() | ||
self._put(val) | ||
|
||
def qsize(self): | ||
""" | ||
Number of items in this queue. | ||
""" | ||
return len(self._queue) | ||
|
||
def empty(self): | ||
""" | ||
Return True if the queue is empty. | ||
""" | ||
return len(self._queue) == 0 | ||
|
||
def full(self): | ||
""" | ||
Return True if there are maxsize items in the queue. | ||
""" | ||
return 0 < self.maxsize <= self.qsize() | ||
|
||
def task_done(self): | ||
""" | ||
Indicate that a formerly enqueued task is complete. | ||
|
||
If a join() is currently blocking, it will resume when all items have | ||
been processed (meaning that a task_done() call was received for every | ||
item that had been put() into the queue). | ||
|
||
Raises ValueError if called more times than there were items placed in | ||
the queue. | ||
""" | ||
if self._active_tasks == 0: | ||
# Can't have less than 0 | ||
raise ValueError("task_done() called too many times") | ||
|
||
self._active_tasks -= 1 | ||
|
||
if self._active_tasks == 0: | ||
_release_task_queue(self._waiting_for_completion) | ||
|
||
async def join(self): | ||
""" | ||
Block until all items in the queue have been gotten and processed. | ||
""" | ||
await _wait_on_task_queue(self._waiting_for_completion) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SPDX-FileCopyrightText: Copyright (c) 2023 Your Name Here / Handle? for Adafruit Industries ? What's the right approach here? it seems wrong copying this copyright verbatim
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per @dhalbert I copied the original copyright
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"for Adafruit Industries" is when someone got paid to do the work.