Skip to content

Commit

Permalink
Suppress warning for deprecation of get_event_loop() (#92)
Browse files Browse the repository at this point in the history
* Fixes deprecation warning

Signed-off-by: Voldivh <eloyabmfcv@gmail.com>
  • Loading branch information
Voldivh authored and clalancette committed Jun 22, 2023
1 parent 17bf60f commit f4bd8fd
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions osrf_pycommon/process_utils/get_loop_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import os
import threading
import warnings

_thread_local = threading.local()

Expand All @@ -24,30 +23,34 @@ def get_loop_impl(asyncio):
if getattr(_thread_local, 'loop_has_been_setup', False):
return asyncio.get_event_loop()
# Setup this thread's loop and return it
if os.name == 'nt':
try:
loop = asyncio.get_event_loop()
try:
loop = asyncio.get_running_loop()
have_running_loop = True
except RuntimeError:
# Thrown when there is no running loop
have_running_loop = False
except AttributeError:
# Thrown when on Python < 3.7
loop = asyncio.get_event_loop()
have_running_loop = True

if have_running_loop:
if os.name == 'nt':
if not isinstance(loop, asyncio.ProactorEventLoop):
# Before replacing the existing loop, explicitly
# close it to prevent an implicit close during
# garbage collection, which may or may not be a
# problem depending on the loop implementation.
loop.close()
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
except (RuntimeError, AssertionError):
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
else:
try:
with warnings.catch_warnings():
warnings.filterwarnings(
'ignore',
'There is no current event loop',
DeprecationWarning)
loop = asyncio.get_event_loop()
except (RuntimeError, AssertionError):
if os.name == 'nt':
loop = asyncio.ProactorEventLoop()
else:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

asyncio.set_event_loop(loop)

_thread_local.loop_has_been_setup = True

return loop

0 comments on commit f4bd8fd

Please sign in to comment.