Skip to content

Commit b4becb1

Browse files
committed
Tutorial: Improve section on polling ThreadSafeFlag.
1 parent f1fff20 commit b4becb1

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

v3/docs/TUTORIAL.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,9 @@ class which allows multiple tasks to wait on it.
10981098

10991099
### 3.6.1 Querying a ThreadSafeFlag
11001100

1101-
The state of a ThreadSafeFlag may be tested as follows:
1101+
The `ThreadSafeFlag` class has no equivalent to `Event.is_set`. A synchronous
1102+
function which returns the state of a `ThreadSafeFlag` instance may be created
1103+
as follows:
11021104
```python
11031105
import asyncio
11041106
from select import poll, POLLIN
@@ -1109,12 +1111,12 @@ async def foo(tsf): # Periodically set the ThreadSafeFlag
11091111
await asyncio.sleep(1)
11101112
tsf.set()
11111113

1112-
def ready(tsf, poller):
1114+
def ready(tsf, poller): # Return a function which returns tsf status
11131115
r = (tsf, POLLIN)
11141116
poller.register(*r)
11151117

11161118
def is_rdy():
1117-
return r in poller.ipoll(0)
1119+
return r in poller.ipoll(0) # Immediate return
11181120

11191121
return is_rdy
11201122

@@ -1136,9 +1138,12 @@ async def test():
11361138
asyncio.run(test())
11371139
```
11381140
The `ready` closure returns a nonblocking function which tests the status of a
1139-
given flag. In the above example `.wait()` is not called until the flag has been
1141+
passed flag. In this example `.wait()` is not called until the flag has been
11401142
set, consequently `.wait()` returns rapidly.
11411143

1144+
The `select.poll` mechanism works because `ThreadSafeFlag` is subclassed from
1145+
`io.IOBase` and has an `ioctl` method.
1146+
11421147
###### [Contents](./TUTORIAL.md#contents)
11431148

11441149
## 3.7 Barrier

0 commit comments

Comments
 (0)