File tree 4 files changed +27
-12
lines changed
4 files changed +27
-12
lines changed Original file line number Diff line number Diff line change @@ -127,3 +127,5 @@ available as the attribute ``.loop``.
127
127
.. autofunction :: fsspec.asyn.sync_wrapper
128
128
129
129
.. autofunction :: fsspec.asyn.get_loop
130
+
131
+ .. autofunction :: fsspec.asyn.fsspec_loop
Original file line number Diff line number Diff line change @@ -4,7 +4,9 @@ Changelog
4
4
Dev
5
5
---
6
6
7
+ Enhancements
7
8
9
+ - Introduce ``fsspec.asyn.fsspec_loop `` to temporarily switch to the fsspec loop.
8
10
9
11
10
12
2021.06.0
Original file line number Diff line number Diff line change @@ -105,6 +105,17 @@ def _selector_policy():
105
105
asyncio .set_event_loop_policy (original_policy )
106
106
107
107
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
+
108
119
def get_loop ():
109
120
"""Create or return the default fsspec IO loop
110
121
@@ -131,16 +142,16 @@ def fsspec_loop():
131
142
terinated.
132
143
"""
133
144
try :
134
- original_loop = asyncio . get_event_loop ()
145
+ original_loop = get_running_loop ()
135
146
except RuntimeError :
136
147
original_loop = None
137
148
138
149
fsspec_loop = get_loop ()
139
150
try :
140
- asyncio .set_event_loop (fsspec_loop )
151
+ asyncio ._set_running_loop (fsspec_loop )
141
152
yield fsspec_loop
142
153
finally :
143
- asyncio .set_event_loop (original_loop )
154
+ asyncio ._set_running_loop (original_loop )
144
155
145
156
146
157
try :
Original file line number Diff line number Diff line change 8
8
9
9
import fsspec
10
10
import fsspec .asyn
11
- from fsspec .asyn import _throttled_gather
11
+ from fsspec .asyn import _throttled_gather , get_running_loop
12
12
13
13
14
14
def test_sync_methods ():
@@ -135,20 +135,20 @@ def test_windows_policy():
135
135
136
136
137
137
def test_fsspec_loop ():
138
- asyncio .set_event_loop (None )
138
+ asyncio ._set_running_loop (None )
139
139
140
140
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 ()
143
143
144
144
with pytest .raises (RuntimeError ):
145
- asyncio . get_event_loop ()
145
+ get_running_loop ()
146
146
147
147
original_loop = asyncio .new_event_loop ()
148
- asyncio .set_event_loop (original_loop )
148
+ asyncio ._set_running_loop (original_loop )
149
149
150
150
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 ()
153
153
154
- assert asyncio . get_event_loop () is original_loop
154
+ assert get_running_loop () is original_loop
You can’t perform that action at this time.
0 commit comments