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

Catch all of the spurious warnings from get_event_loop. #94

Merged
merged 1 commit into from
Jul 22, 2023
Merged
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
71 changes: 36 additions & 35 deletions osrf_pycommon/process_utils/get_loop_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,43 @@


def get_loop_impl(asyncio):
global _thread_local
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()
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()
# See the note in
# https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop # noqa
# In short, between Python 3.10.0 and 3.10.8, this unconditionally raises a
# DeprecationWarning. But after 3.10.8, it only raises the warning if
# ther eis no current loop set in the policy. Since we are setting a loop
# in the policy, this warning is spurious, and will go away once we get
# away from Python 3.10.6 (verified with Python 3.11.3).
with warnings.catch_warnings():
warnings.filterwarnings(
'ignore',
'There is no current event loop',
DeprecationWarning)

global _thread_local
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()
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)
except (RuntimeError, AssertionError):
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
else:
try:
# See the note in
# https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop # noqa
# In short, between Python 3.10.0 and 3.10.8, this unconditionally
# raises a DeprecationWarning. But after 3.10.8, it only raises a
# the warning if there is no current loop set in the policy.
# Since we are setting a loop in the policy, this warning is
# spurious, and will go away once we get away from Python 3.10.6
# (verified with Python 3.11.3).
with warnings.catch_warnings():
warnings.filterwarnings(
'ignore',
'There is no current event loop',
DeprecationWarning)
else:
try:
loop = asyncio.get_event_loop()
except (RuntimeError, AssertionError):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
_thread_local.loop_has_been_setup = True
except (RuntimeError, AssertionError):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
_thread_local.loop_has_been_setup = True

return loop