This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mykola-mokhnach
Recipients asvetlov, mykola-mokhnach, yselivanov
Date 2021-09-09.04:46:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1631162776.4.0.536124821312.issue45146@roundup.psfhosted.org>
In-reply-to
Content
Currently the asyncio.Condition class does not provide any properties that would allow to determine how many (if) consumers are waiting for the current condition. My scenario is the following:

```
...
FILE_STATS_CONDITIONS: Dict[str, asyncio.Condition] = {}
FILE_STATS_CACHE: Dict[str, Union[FileStat, Exception]] = {}

...

    async def _fetch_file_stat(self: T, file_id: str) -> FileStat:
        """
        The idea behind this caching is to avoid sending multiple info requests
        for the same file id to the server and thus to avoid throttling. This is safe, because
        stored files are immutable.
        """
        try:
            file_stat: FileStat
            if file_id in FILE_STATS_CONDITIONS:
                self.log.debug(f'Reusing the previously cached stat request for the file {file_id}')
                async with FILE_STATS_CONDITIONS[file_id]:
                    await FILE_STATS_CONDITIONS[file_id].wait()
                    cached_result = FILE_STATS_CACHE[file_id]
                    if isinstance(cached_result, FileStat):
                        file_stat = cached_result
                    else:
                        raise cached_result
            else:
                FILE_STATS_CONDITIONS[file_id] = asyncio.Condition()
                cancellation_exception: Optional[asyncio.CancelledError] = None
                async with FILE_STATS_CONDITIONS[file_id]:
                    file_stat_task = asyncio.create_task(self.storage_client.get_file_stat(file_id))
                    try:
                        try:
                            file_stat = await asyncio.shield(file_stat_task)
                        except asyncio.CancelledError as e:
                            if len(getattr(FILE_STATS_CONDITIONS[file_id], '_waiters', (None,))) == 0:
                                # it is safe to cancel now if there are no consumers in the waiting queue
                                file_stat_task.cancel()
                                raise
                            # we don't want currently waiting consumers to fail because of the task cancellation
                            file_stat = await file_stat_task
                            cancellation_exception = e
                        FILE_STATS_CACHE[file_id] = file_stat
                    except Exception as e:
                        FILE_STATS_CACHE[file_id] = e
                        raise
                    finally:
                        FILE_STATS_CONDITIONS[file_id].notify_all()
                if cancellation_exception is not None:
                    raise cancellation_exception
            # noinspection PyUnboundLocalVariable
            self.log.info(f'File stat: {file_stat}')
            return file_stat
        except ObjectNotFoundError:
            self.log.info(f'The file identified by "{file_id}" either does not exist or has expired')
            raise file_not_found_error(file_id)
        finally:
            if file_id in FILE_STATS_CONDITIONS and not FILE_STATS_CONDITIONS[file_id].locked():
                del FILE_STATS_CONDITIONS[file_id]
                if file_id in FILE_STATS_CACHE:
                    del FILE_STATS_CACHE[file_id]
```

Basically I need to use `getattr(FILE_STATS_CONDITIONS[file_id], '_waiters', (None,))` to workaround this limitation in order to figure out whether to cancel my producer now or later.

It would be nice to have a public property on the Condition class, which would basically return the value of `len(condition._waiters)`.
History
Date User Action Args
2021-09-09 04:46:16mykola-mokhnachsetrecipients: + mykola-mokhnach, asvetlov, yselivanov
2021-09-09 04:46:16mykola-mokhnachsetmessageid: <1631162776.4.0.536124821312.issue45146@roundup.psfhosted.org>
2021-09-09 04:46:16mykola-mokhnachlinkissue45146 messages
2021-09-09 04:46:16mykola-mokhnachcreate