Skip to content

Commit 45f51dd

Browse files
committed
Implement get_running_loop().
1 parent f5c4bca commit 45f51dd

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

FASTPOLL.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ formerly provided by `asyncio_priority.py` is now implemented.
6767
3.2 [Low Priority](./FASTPOLL.md#32-low-priority)
6868
3.3 [Other Features](./FASTPOLL.md#33-other-features)
6969
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)
7171
3.3.3 [StreamReader readinto method](./FASTPOLL.md#333-streamreader-readinto-method)
7272
3.4 [Low priority yield](./FASTPOLL.md#34-low-priority-yield)
7373
3.4.1 [Task Cancellation and Timeouts](./FASTPOLL.md#341-task-cancellation-and-timeouts)
@@ -314,7 +314,7 @@ Arguments to `get_event_loop()`:
314314

315315
Device drivers which are to be capable of running at high priority should be
316316
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).
318318

319319
The `fast_io` version will schedule I/O whenever the `ioctl` reports a ready
320320
status. This implies that devices which become ready very soon after being
@@ -358,30 +358,31 @@ Variable:
358358
* `version` Returns a 2-tuple. Current contents ('fast_io', '0.25'). Enables
359359
the presence and realease state of this version to be determined at runtime.
360360

361-
### 3.3.2 got_event_loop
361+
### 3.3.2 Check event loop status
362362

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:
377366
```python
378367
import uasyncio as asyncio
379368
bar = Bar() # Constructor calls get_event_loop()
380369
# and renders these args inoperative
381370
loop = asyncio.get_event_loop(runq_len=40, waitq_len=40)
382371
```
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.
385386

386387
### 3.3.3 StreamReader readinto method
387388

fast_io/core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,12 @@ def get_event_loop(runq_len=16, waitq_len=16, ioq_len=0, lp_len=0):
342342
return _event_loop
343343

344344
# Allow user classes to determine prior event loop instantiation.
345-
def got_event_loop():
345+
def get_running_loop():
346+
if _event_loop is None:
347+
raise RuntimeError('Event loop not instantiated')
348+
return _event_loop
349+
350+
def got_event_loop(): # Kept to avoid breaking code
346351
return _event_loop is not None
347352

348353
def sleep(secs):

0 commit comments

Comments
 (0)