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
@@ -2238,49 +2243,64 @@ ValueError: malformed node or string
2238
2243
```
2239
2244
2240
2245
2241
-
Coroutine
2242
-
---------
2243
-
***Any function that contains a `'(yield)'` expression returns a coroutine.**
2244
-
***Coroutines are similar to iterators, but data needs to be pulled out of an iterator by calling `'next(<iter>)'`, while we push data into the coroutine by calling `'<coroutine>.send(<el>)'`.**
2245
-
***Coroutines provide more powerful data routing possibilities than iterators.**
2246
+
Coroutines
2247
+
----------
2248
+
***Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don’t use as much memory.**
2249
+
***Coroutine definition starts with `'async'` and its call with `'await'`.**
2250
+
***`'asyncio.run(<coroutine>)'` is the main entry point for asynchronous programs.**
2251
+
***Fucntions wait(), gather() and as_completed() can be used when multiple coroutines need to be started at the same time.**
2246
2252
2247
-
### Helper Decorator
2248
-
***All coroutines must first be "primed" by calling `'next(<coroutine>)'`.**
2249
-
***Remembering to call next() is easy to forget.**
2250
-
***Solved by wrapping coroutine functions with the following decorator:**
2253
+
#### Starts a terminal game where you control an asterisk that must avoid numbers:
2251
2254
2252
2255
```python
2253
-
defcoroutine(func):
2254
-
defout(*args, **kwargs):
2255
-
cr = func(*args, **kwargs)
2256
-
next(cr)
2257
-
return cr
2258
-
return out
2259
-
```
2256
+
import asyncio, collections, curses, enum, random
2260
2257
2261
-
### Pipeline Example
2262
-
```python
2263
-
defreader(target):
2264
-
for i inrange(10):
2265
-
target.send(i)
2266
-
target.close()
2258
+
P = collections.namedtuple('P', 'x y') # Position
2259
+
D = enum.Enum('D', 'n e s w') # Direction
2267
2260
2268
-
@coroutine
2269
-
defadder(target):
2261
+
defmain(screen):
2262
+
curses.curs_set(0) # Makes cursor invisible.
2263
+
screen.nodelay(True) # Makes getch() non-blocking.
0 commit comments