Skip to content

Commit

Permalink
Allowed Event to be set before it's bound to an async backend (#835)
Browse files Browse the repository at this point in the history
Fixes #819.
  • Loading branch information
agronholm authored Dec 5, 2024
1 parent 93a5746 commit f316ce5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
3 changes: 3 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
an object with a ``.fileno()`` method or an integer handle, and deprecated
their now obsolete versions (``wait_socket_readable()`` and
``wait_socket_writable()``) (PR by @davidbrochart)
- Changed ``EventAdapter`` (an ``Event`` with no bound async backend) to allow ``set()``
to work even before an async backend is bound to it
(`#819 <https://github.com/agronholm/anyio/issues/819>`_)
- Added support for ``wait_readable()`` and ``wait_writable()`` on ``ProactorEventLoop``
(used on asyncio + Windows by default)
- Fixed the return type annotations of ``readinto()`` and ``readinto1()`` methods in the
Expand Down
13 changes: 11 additions & 2 deletions src/anyio/_core/_synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def statistics(self) -> EventStatistics:

class EventAdapter(Event):
_internal_event: Event | None = None
_is_set: bool = False

def __new__(cls) -> EventAdapter:
return object.__new__(cls)
Expand All @@ -117,14 +118,22 @@ def __new__(cls) -> EventAdapter:
def _event(self) -> Event:
if self._internal_event is None:
self._internal_event = get_async_backend().create_event()
if self._is_set:
self._internal_event.set()

return self._internal_event

def set(self) -> None:
self._event.set()
if self._internal_event is None:
self._is_set = True
else:
self._event.set()

def is_set(self) -> bool:
return self._internal_event is not None and self._internal_event.is_set()
if self._internal_event is None:
return self._is_set

return self._internal_event.is_set()

async def wait(self) -> None:
await self._event.wait()
Expand Down
11 changes: 6 additions & 5 deletions tests/test_synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,16 +316,17 @@ async def waiter() -> None:
def test_instantiate_outside_event_loop(
self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
) -> None:
async def use_event() -> None:
event.set()
await event.wait()

event = Event()
assert not event.is_set()
assert event.statistics().tasks_waiting == 0

event.set()
assert event.is_set()

run(
use_event, backend=anyio_backend_name, backend_options=anyio_backend_options
event.wait,
backend=anyio_backend_name,
backend_options=anyio_backend_options,
)


Expand Down

0 comments on commit f316ce5

Please sign in to comment.