@@ -67,7 +67,7 @@ formerly provided by `asyncio_priority.py` is now implemented.
67
67
3.2 [ Low Priority] ( ./FASTPOLL.md#32-low-priority )
68
68
3.3 [ Other Features] ( ./FASTPOLL.md#33-other-features )
69
69
3.3.1 [ Version] ( ./FASTPOLL.md#331-version )
70
- 3.3.2 [ got_event_loop ] ( ./FASTPOLL.md#332-got_event_loop )
70
+ 3.3.2 [ Check event loop status ] ( ./FASTPOLL.md#332-check-event-loop-status )
71
71
3.3.3 [ StreamReader readinto method] ( ./FASTPOLL.md#333-streamreader-readinto-method )
72
72
3.4 [ Low priority yield] ( ./FASTPOLL.md#34-low-priority-yield )
73
73
3.4.1 [ Task Cancellation and Timeouts] ( ./FASTPOLL.md#341-task-cancellation-and-timeouts )
@@ -314,7 +314,7 @@ Arguments to `get_event_loop()`:
314
314
315
315
Device drivers which are to be capable of running at high priority should be
316
316
written to use stream I/O: see
317
- [ Writing streaming device drivers] ( ./TUTORIAL.md#54 -writing-streaming-device-drivers ) .
317
+ [ Writing streaming device drivers] ( ./TUTORIAL.md#64 -writing-streaming-device-drivers ) .
318
318
319
319
The ` fast_io ` version will schedule I/O whenever the ` ioctl ` reports a ready
320
320
status. This implies that devices which become ready very soon after being
@@ -358,30 +358,31 @@ Variable:
358
358
* ` version ` Returns a 2-tuple. Current contents ('fast_io', '0.25'). Enables
359
359
the presence and realease state of this version to be determined at runtime.
360
360
361
- ### 3.3.2 got_event_loop
361
+ ### 3.3.2 Check event loop status
362
362
363
- Function:
364
- * ` got_event_loop() ` No arg. Returns a ` bool ` : ` True ` if the event loop has
365
- been instantiated. Enables code using the event loop to raise an exception if
366
- the event loop was not instantiated:
367
- ``` python
368
- class Foo ():
369
- def __init__ (self ):
370
- if asyncio.got_event_loop():
371
- loop = asyncio.get_event_loop()
372
- loop.create_task(self ._run())
373
- else :
374
- raise OSError (' Foo class requires an event loop instance' )
375
- ```
376
- This avoids subtle errors:
363
+ The way ` uasyncio ` works can lead to subtle bugs. The first call to
364
+ ` get_event_loop ` instantiates the event loop and determines the size of its
365
+ queues. Hence the following code will not behave as expected:
377
366
``` python
378
367
import uasyncio as asyncio
379
368
bar = Bar() # Constructor calls get_event_loop()
380
369
# and renders these args inoperative
381
370
loop = asyncio.get_event_loop(runq_len = 40 , waitq_len = 40 )
382
371
```
383
- This is mainly for retro-fitting to existing classes and functions. The
384
- preferred approach is to pass the event loop to classes as a constructor arg.
372
+ CPython V3.7 provides a function ` get_running_loop ` which enables the current
373
+ loop to be retrieved, raising a ` RuntimeError ` if one has not been
374
+ instantiated. This is provided in ` fast_io ` . In the above sample the ` Bar `
375
+ constructor call ` get_running_loop ` to avoid inadvertently instantiating an
376
+ event loop with default args.
377
+
378
+ Function:
379
+ * ` get_running_loop ` No arg. Returns the event loop or raises a ` RuntimeError `
380
+ if one has not been instantiated.
381
+
382
+ Function:
383
+ * ` got_event_loop() ` No arg. Returns a ` bool ` : ` True ` if the event loop has
384
+ been instantiated. This is retained for compatibility: ` get_running_loop ` is
385
+ preferred.
385
386
386
387
### 3.3.3 StreamReader readinto method
387
388
0 commit comments