You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39-9
Original file line number
Diff line number
Diff line change
@@ -81,16 +81,16 @@ from aio_stdout import IOLock, ainput, aprint
81
81
82
82
asyncdefcountdown(n: int) -> None:
83
83
"""Count down from `n`, taking `n` seconds to run."""
84
-
asyncwith IOLock(n=5) asio_lock:
84
+
asyncwith IOLock(n=5) aslock:
85
85
for i inrange(n, 0, -1):
86
-
awaitio_lock.aprint(i)
86
+
awaitlock.aprint(i)
87
87
await asyncio.sleep(1)
88
88
89
89
asyncdefget_name() -> str:
90
90
"""Ask the user for their name."""
91
-
asyncwith IOLock() asio_lock:
92
-
name =awaitio_lock.ainput("What is your name? ")
93
-
awaitio_lock.aprint(f"Your name is {name}.")
91
+
asyncwith IOLock() aslock:
92
+
name =awaitlock.ainput("What is your name? ")
93
+
awaitlock.aprint(f"Your name is {name}.")
94
94
return name
95
95
96
96
asyncdefmain() -> None:
@@ -122,7 +122,7 @@ Your name is Jane.
122
122
1
123
123
```
124
124
125
-
Notice that this time the `countdown` does not immediately yield to the `get_name`. Instead, it runs 5 messages before yielding control over to `get_name`. Now, after the `ainput` finishes, it does not yield to `countdown`. Instead, it runs its own `aprint` first. In the meantime, `countdown` continues to run in the background and flushes all of its buffered messages afterwards.
125
+
Notice that this time the `countdown` does not immediately yield to the `get_name`. Instead, it runs 5 messages before yielding control over to `get_name`. Now, after the `lock.ainput` finishes, it does not yield to `countdown`. Instead, it runs its own `lock.aprint` first. In the meantime, `countdown` continues to run in the background and flushes all of its buffered messages afterwards.
126
126
127
127
Flushing
128
128
---------
@@ -132,13 +132,43 @@ Since messages may be delayed, it is possible for your asynchronous code to fini
132
132
```python
133
133
from aio_stdout import flush
134
134
135
+
@flush
135
136
asyncdefmain() -> None:
136
-
asyncwith flush:
137
-
pass
137
+
...
138
+
```
139
+
140
+
Final Example
141
+
-------------
142
+
143
+
Combining all best practices, the final example should look something like this:
144
+
145
+
```python
146
+
import asyncio
147
+
from aio_stdout import IOLock, ainput, aprint, flush
148
+
149
+
asyncdefcountdown(n: int) -> None:
150
+
"""Count down from `n`, taking `n` seconds to run."""
151
+
for i inrange(n, 0, -1):
152
+
await aprint(i)
153
+
await asyncio.sleep(1)
154
+
155
+
asyncdefget_name() -> str:
156
+
"""Ask the user for their name."""
157
+
asyncwith IOLock() as lock:
158
+
name =await lock.ainput("What is your name? ")
159
+
await lock.aprint(f"Your name is {name}.")
160
+
return name
161
+
162
+
@flush
163
+
asyncdefmain() -> None:
164
+
await asyncio.gather(countdown(15), get_name())
165
+
166
+
if__name__=="__main__":
167
+
asyncio.run(main())
138
168
```
139
169
140
170
Common Gotchas
141
171
---------------
142
172
143
173
- Using `input` or `print` instead of `ainput` and `aprint` will push a message immediately to the console, potentially conflicting with `ainput` or `aprint`.
144
-
- Using `ainput` or `aprint` instead of `io_lock.ainput` and `io_lock.aprint` may produce **deadlock** due to having to wait for the lock to release. As such, the `io_lock` is equipped with a default `timeout` limit of 10 seconds to avoid deadlock and explain to users this potential problem.
174
+
- Using `ainput` or `aprint` instead of `lock.ainput` and `lock.aprint` may produce **deadlock** due to having to wait for the lock to release. As such, the `lock` is equipped with a default `timeout` limit of 10 seconds to avoid deadlock and explain to users this potential problem.
0 commit comments