Skip to content

Queue task_done/join enhancement #108

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

Closed
stephanelsmith opened this issue Jul 17, 2023 · 3 comments
Closed

Queue task_done/join enhancement #108

stephanelsmith opened this issue Jul 17, 2023 · 3 comments

Comments

@stephanelsmith
Copy link
Contributor

Hello @peterhinch,

In my project, I've found itility in the task_done/join feature in Queue:
https://docs.python.org/3/library/asyncio-queue.html

I have something like:

    def _put(self, val):
        self._jncnt += 1
        self._evput.set()  # Schedule tasks waiting on put
        self._evput.clear()
        self._queue.append(val)

    def task_done(self):
        self._jncnt -= 1
        if self._jncnt <= 0:
            self._jnevt.set()
        else:
            self._jnevt.clear()

    async def join(self):
        await self._jnevt.wait()

If there's interest, happy to create a PR for this.

@stephanelsmith
Copy link
Contributor Author

stephanelsmith commented Jul 17, 2023

Related but seperate, my original implementation idea was semaphore like, essentially acquiring (incrementing) on each put, and then releasing on task_done. I had an issue with it, essentially, my program was exiting on event.wait().... Is it just me or have you seen this?

I would expect this to hang. On Python3 it does, but on micropython it exits without message on both unix and esp32 ports.

import asyncio
from asyncio import Event

async def main():
    print('start')
    event = Event()
    await event.wait()
    print('done')

asyncio.run(main())

I see the same thing if I add wait before event is set, like this, but the wait could be in the waiter as well.

async def main():
    event = Event()
    asyncio.create_task(waiter(event))
    await event.wait()  # Pause here until event is set
    await asyncio.sleep(2)
    print('Setting event')

@peterhinch
Copy link
Owner

Re Queue methods I would be interested in a PR, ideally with asyntest.py adapted to test these methods.

I agree the first example should hang and can confirm that it doesn't. I'm not clear about the second example - what is the waiter task? In any event the first warrants raising an issue in my opinion.

@stephanelsmith
Copy link
Contributor Author

stephanelsmith commented Jul 18, 2023

I'll put together a PR with associated test, no problem.

For the second example, what I did not mention and should have is that is reproducable using the Event example from the documentation as a starting point. A little more 'in application' I guess.
https://github.com/peterhinch/micropython-async/blob/master/v3/docs/TUTORIAL.md#32-event

The initial example is probably better for raisining an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants