Skip to content

Commit 7487861

Browse files
authored
Merge pull request #1668 from minrk/deprecate-get-event-loop
avoid calls to deprecated asyncio.get_event_loop
2 parents fe18dc5 + 7580628 commit 7487861

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

examples/asyncio/coroutines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async def sender() -> None:
4545
await asyncio.sleep(1)
4646

4747

48-
asyncio.get_event_loop().run_until_complete(
48+
asyncio.run(
4949
asyncio.wait(
5050
[
5151
ping(),

examples/asyncio/helloworld_pubsub_dealerrouter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self, url: str = '127.0.0.1', port: int = 5555):
6161
def main(self) -> None:
6262

6363
# activate publishers / subscribers
64-
asyncio.get_event_loop().run_until_complete(
64+
asyncio.run(
6565
asyncio.wait(
6666
[
6767
self.hello_world_pub(),

zmq/asyncio.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ class _AsyncIO:
100100
_READ = selectors.EVENT_READ
101101

102102
def _default_loop(self):
103+
if sys.version_info >= (3, 7):
104+
try:
105+
return asyncio.get_running_loop()
106+
except RuntimeError:
107+
warnings.warn(
108+
"No running event loop. zmq.asyncio should be used from within an asyncio loop.",
109+
RuntimeWarning,
110+
stacklevel=4,
111+
)
112+
# get_event_loop deprecated in 3.10:
103113
return asyncio.get_event_loop()
104114

105115

zmq/tests/test_asyncio.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ def run(self):
3535
async def never_ending_task(socket):
3636
await socket.recv() # never ever receive anything
3737

38-
loop = asyncio.get_event_loop()
38+
loop = asyncio.new_event_loop()
3939
coro = asyncio.wait_for(never_ending_task(socket), timeout=1)
4040
try:
4141
loop.run_until_complete(coro)
4242
except asyncio.TimeoutError:
4343
pass # expected timeout
4444
else:
4545
assert False, "never_ending_task was completed unexpectedly"
46+
finally:
47+
loop.close()
4648

4749

4850
class TestAsyncIOSocket(BaseZMQTestCase):
@@ -389,7 +391,7 @@ async def test():
389391
r.close()
390392
w.close()
391393

392-
loop = asyncio.get_event_loop()
394+
loop = asyncio.new_event_loop()
393395
loop.run_until_complete(test())
394396

395397
def test_multiple_loops(self):

0 commit comments

Comments
 (0)