Skip to content

Commit 7550e75

Browse files
committed
document the new changes
1 parent 07b47ef commit 7550e75

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

docs/source/async.rst

+2
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ available as the attribute ``.loop``.
127127
.. autofunction:: fsspec.asyn.sync_wrapper
128128

129129
.. autofunction:: fsspec.asyn.get_loop
130+
131+
.. autofunction:: fsspec.asyn.fsspec_loop

docs/source/changelog.rst

+2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Changelog
44
Dev
55
---
66

7+
Enhancements
78

9+
- Introduce ``fsspec.asyn.fsspec_loop`` to temporarily switch to the fsspec loop.
810

911

1012
2021.06.0

fsspec/asyn.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ def _selector_policy():
105105
asyncio.set_event_loop_policy(original_policy)
106106

107107

108+
def get_running_loop():
109+
if hasattr(asyncio, "get_running_loop"):
110+
return asyncio.get_running_loop()
111+
else:
112+
loop = asyncio._get_running_loop()
113+
if loop is None:
114+
raise RuntimeError("no running event loop")
115+
else:
116+
return loop
117+
118+
108119
def get_loop():
109120
"""Create or return the default fsspec IO loop
110121
@@ -131,16 +142,16 @@ def fsspec_loop():
131142
terinated.
132143
"""
133144
try:
134-
original_loop = asyncio.get_event_loop()
145+
original_loop = get_running_loop()
135146
except RuntimeError:
136147
original_loop = None
137148

138149
fsspec_loop = get_loop()
139150
try:
140-
asyncio.set_event_loop(fsspec_loop)
151+
asyncio._set_running_loop(fsspec_loop)
141152
yield fsspec_loop
142153
finally:
143-
asyncio.set_event_loop(original_loop)
154+
asyncio._set_running_loop(original_loop)
144155

145156

146157
try:

fsspec/tests/test_async.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import fsspec
1010
import fsspec.asyn
11-
from fsspec.asyn import _throttled_gather
11+
from fsspec.asyn import _throttled_gather, get_running_loop
1212

1313

1414
def test_sync_methods():
@@ -135,20 +135,20 @@ def test_windows_policy():
135135

136136

137137
def test_fsspec_loop():
138-
asyncio.set_event_loop(None)
138+
asyncio._set_running_loop(None)
139139

140140
with fsspec.asyn.fsspec_loop() as loop:
141-
assert asyncio.get_event_loop() is loop
142-
assert asyncio.get_event_loop() is fsspec.asyn.get_loop()
141+
assert get_running_loop() is loop
142+
assert get_running_loop() is fsspec.asyn.get_loop()
143143

144144
with pytest.raises(RuntimeError):
145-
asyncio.get_event_loop()
145+
get_running_loop()
146146

147147
original_loop = asyncio.new_event_loop()
148-
asyncio.set_event_loop(original_loop)
148+
asyncio._set_running_loop(original_loop)
149149

150150
with fsspec.asyn.fsspec_loop() as loop:
151-
assert asyncio.get_event_loop() is loop
152-
assert asyncio.get_event_loop() is fsspec.asyn.get_loop()
151+
assert get_running_loop() is loop
152+
assert get_running_loop() is fsspec.asyn.get_loop()
153153

154-
assert asyncio.get_event_loop() is original_loop
154+
assert get_running_loop() is original_loop

0 commit comments

Comments
 (0)