Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid calls to deprecated asyncio.get_event_loop #1668

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/asyncio/coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def sender() -> None:
await asyncio.sleep(1)


asyncio.get_event_loop().run_until_complete(
asyncio.run(
asyncio.wait(
[
ping(),
Expand Down
2 changes: 1 addition & 1 deletion examples/asyncio/helloworld_pubsub_dealerrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, url: str = '127.0.0.1', port: int = 5555):
def main(self) -> None:

# activate publishers / subscribers
asyncio.get_event_loop().run_until_complete(
asyncio.run(
asyncio.wait(
[
self.hello_world_pub(),
Expand Down
10 changes: 10 additions & 0 deletions zmq/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ class _AsyncIO:
_READ = selectors.EVENT_READ

def _default_loop(self):
if sys.version_info >= (3, 7):
try:
return asyncio.get_running_loop()
except RuntimeError:
warnings.warn(
"No running event loop. zmq.asyncio should be used from within an asyncio loop.",
RuntimeWarning,
stacklevel=4,
)
# get_event_loop deprecated in 3.10:
return asyncio.get_event_loop()


Expand Down
6 changes: 4 additions & 2 deletions zmq/tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ def run(self):
async def never_ending_task(socket):
await socket.recv() # never ever receive anything

loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
coro = asyncio.wait_for(never_ending_task(socket), timeout=1)
try:
loop.run_until_complete(coro)
except asyncio.TimeoutError:
pass # expected timeout
else:
assert False, "never_ending_task was completed unexpectedly"
finally:
loop.close()


class TestAsyncIOSocket(BaseZMQTestCase):
Expand Down Expand Up @@ -389,7 +391,7 @@ async def test():
r.close()
w.close()

loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
loop.run_until_complete(test())

def test_multiple_loops(self):
Expand Down