An event blocks coroutines until its internal flag is set to True. Similar to `threading.Event`. A coroutine can wait for an event to be set. Once it is set, calls to ``yield event.wait()`` will not block unless the event has been cleared: .. testcode:: import asyncio
| 156 | |
| 157 | |
| 158 | class Event(object): |
| 159 | """An event blocks coroutines until its internal flag is set to True. |
| 160 | |
| 161 | Similar to `threading.Event`. |
| 162 | |
| 163 | A coroutine can wait for an event to be set. Once it is set, calls to |
| 164 | ``yield event.wait()`` will not block unless the event has been cleared: |
| 165 | |
| 166 | .. testcode:: |
| 167 | |
| 168 | import asyncio |
| 169 | from tornado import gen |
| 170 | from tornado.locks import Event |
| 171 | |
| 172 | event = Event() |
| 173 | |
| 174 | async def waiter(): |
| 175 | print("Waiting for event") |
| 176 | await event.wait() |
| 177 | print("Not waiting this time") |
| 178 | await event.wait() |
| 179 | print("Done") |
| 180 | |
| 181 | async def setter(): |
| 182 | print("About to set the event") |
| 183 | event.set() |
| 184 | |
| 185 | async def runner(): |
| 186 | await gen.multi([waiter(), setter()]) |
| 187 | |
| 188 | asyncio.run(runner()) |
| 189 | |
| 190 | .. testoutput:: |
| 191 | |
| 192 | Waiting for event |
| 193 | About to set the event |
| 194 | Not waiting this time |
| 195 | Done |
| 196 | """ |
| 197 | |
| 198 | def __init__(self) -> None: |
| 199 | self._value = False |
| 200 | self._waiters = set() # type: Set[Future[None]] |
| 201 | |
| 202 | def __repr__(self) -> str: |
| 203 | return "<%s %s>" % ( |
| 204 | self.__class__.__name__, |
| 205 | "set" if self.is_set() else "clear", |
| 206 | ) |
| 207 | |
| 208 | def is_set(self) -> bool: |
| 209 | """Return ``True`` if the internal flag is true.""" |
| 210 | return self._value |
| 211 | |
| 212 | def set(self) -> None: |
| 213 | """Set the internal flag to ``True``. All waiters are awakened. |
| 214 | |
| 215 | Calling `.wait` once the flag is set will not block. |
no outgoing calls