Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64dcf5b

Browse files
gh-93453: Make get_event_loop() an alias of get_running_loop()
1 parent b013804 commit 64dcf5b

16 files changed

+105
-321
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,12 @@ an event loop:
3838

3939
.. function:: get_event_loop()
4040

41-
Get the current event loop.
41+
An alias of :func:`get_running_loop`.
4242

43-
If there is no current event loop set in the current OS thread,
44-
the OS thread is main, and :func:`set_event_loop` has not yet
45-
been called, asyncio will create a new event loop and set it as the
46-
current one.
47-
48-
Because this function has rather complex behavior (especially
49-
when custom event loop policies are in use), using the
50-
:func:`get_running_loop` function is preferred to :func:`get_event_loop`
51-
in coroutines and callbacks.
52-
53-
Consider also using the :func:`asyncio.run` function instead of using
54-
lower level functions to manually create and close an event loop.
55-
56-
.. deprecated:: 3.10
57-
Deprecation warning is emitted if there is no running event loop.
58-
In future Python releases, this function will be an alias of
59-
:func:`get_running_loop`.
43+
.. versionchanged:: 3.12
44+
Prior to Python 3.12, this function created a new event loop if there
45+
was no running event loop and set it as a current event loop for the
46+
current OS thread.
6047

6148
.. function:: set_event_loop(loop)
6249

@@ -66,7 +53,7 @@ an event loop:
6653

6754
Create and return a new event loop object.
6855

69-
Note that the behaviour of :func:`get_event_loop`, :func:`set_event_loop`,
56+
Note that the behaviour of :func:`set_event_loop`
7057
and :func:`new_event_loop` functions can be altered by
7158
:ref:`setting a custom event loop policy <asyncio-policies>`.
7259

@@ -1663,7 +1650,7 @@ event loop::
16631650
print('Hello World')
16641651
loop.stop()
16651652

1666-
loop = asyncio.get_event_loop()
1653+
loop = asyncio.new_event_loop()
16671654

16681655
# Schedule a call to hello_world()
16691656
loop.call_soon(hello_world, loop)
@@ -1699,7 +1686,7 @@ after 5 seconds, and then stops the event loop::
16991686
else:
17001687
loop.stop()
17011688

1702-
loop = asyncio.get_event_loop()
1689+
loop = asyncio.new_event_loop()
17031690

17041691
# Schedule the first call to display_date()
17051692
end_time = loop.time() + 5.0
@@ -1731,7 +1718,7 @@ Wait until a file descriptor received some data using the
17311718
# Create a pair of connected file descriptors
17321719
rsock, wsock = socketpair()
17331720

1734-
loop = asyncio.get_event_loop()
1721+
loop = asyncio.new_event_loop()
17351722

17361723
def reader():
17371724
data = rsock.recv(100)

Doc/library/asyncio-llapi-index.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Obtaining the Event Loop
1919
- The **preferred** function to get the running event loop.
2020

2121
* - :func:`asyncio.get_event_loop`
22-
- Get an event loop instance (current or via the policy).
22+
- An alias of :func:`asyncio.get_running_loop`.
2323

2424
* - :func:`asyncio.set_event_loop`
2525
- Set the event loop as current via the current policy.
@@ -267,7 +267,7 @@ See also the main documentation section about the
267267

268268
.. rubric:: Examples
269269

270-
* :ref:`Using asyncio.get_event_loop() and loop.run_forever()
270+
* :ref:`Using asyncio.get_running_loop() and loop.run_forever()
271271
<asyncio_example_lowlevel_helloworld>`.
272272

273273
* :ref:`Using loop.call_later() <asyncio_example_call_later>`.
@@ -499,7 +499,7 @@ Event Loop Policies
499499
===================
500500

501501
Policies is a low-level mechanism to alter the behavior of
502-
functions like :func:`asyncio.get_event_loop`. See also
502+
functions like :func:`asyncio.new_event_loop`. See also
503503
the main :ref:`policies section <asyncio-policies>` for more
504504
details.
505505

Doc/library/asyncio-policy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ separate event loop per context. The default policy
1616
defines *context* to be the current thread.
1717

1818
By using a custom event loop policy, the behavior of
19-
:func:`get_event_loop`, :func:`set_event_loop`, and
19+
:func:`set_event_loop` and
2020
:func:`new_event_loop` functions can be customized.
2121

2222
Policy objects should implement the APIs defined

Lib/asyncio/events.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -772,27 +772,7 @@ def set_event_loop_policy(policy):
772772
_event_loop_policy = policy
773773

774774

775-
def get_event_loop():
776-
"""Return an asyncio event loop.
777-
778-
When called from a coroutine or a callback (e.g. scheduled with call_soon
779-
or similar API), this function will always return the running event loop.
780-
781-
If there is no running event loop set, the function will return
782-
the result of `get_event_loop_policy().get_event_loop()` call.
783-
"""
784-
# NOTE: this function is implemented in C (see _asynciomodule.c)
785-
return _py__get_event_loop()
786-
787-
788-
def _get_event_loop(stacklevel=3):
789-
current_loop = _get_running_loop()
790-
if current_loop is not None:
791-
return current_loop
792-
import warnings
793-
warnings.warn('There is no current event loop',
794-
DeprecationWarning, stacklevel=stacklevel)
795-
return get_event_loop_policy().get_event_loop()
775+
get_event_loop = get_running_loop
796776

797777

798778
def set_event_loop(loop):
@@ -820,22 +800,20 @@ def set_child_watcher(watcher):
820800
_py__get_running_loop = _get_running_loop
821801
_py__set_running_loop = _set_running_loop
822802
_py_get_running_loop = get_running_loop
823-
_py_get_event_loop = get_event_loop
824-
_py__get_event_loop = _get_event_loop
825803

826804

827805
try:
828806
# get_event_loop() is one of the most frequently called
829807
# functions in asyncio. Pure Python implementation is
830808
# about 4 times slower than C-accelerated.
831809
from _asyncio import (_get_running_loop, _set_running_loop,
832-
get_running_loop, get_event_loop, _get_event_loop)
810+
get_running_loop)
833811
except ImportError:
834812
pass
835813
else:
836814
# Alias C implementations for testing purposes.
837815
_c__get_running_loop = _get_running_loop
838816
_c__set_running_loop = _set_running_loop
839817
_c_get_running_loop = get_running_loop
840-
_c_get_event_loop = get_event_loop
841-
_c__get_event_loop = _get_event_loop
818+
819+
get_event_loop = get_running_loop

Lib/asyncio/futures.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ def __init__(self, *, loop=None):
7878
the default event loop.
7979
"""
8080
if loop is None:
81-
self._loop = events._get_event_loop()
82-
else:
83-
self._loop = loop
81+
loop = events.get_running_loop()
82+
self._loop = loop
8483
self._callbacks = []
8584
if self._loop.get_debug():
8685
self._source_traceback = format_helpers.extract_stack(
@@ -416,7 +415,7 @@ def wrap_future(future, *, loop=None):
416415
assert isinstance(future, concurrent.futures.Future), \
417416
f'concurrent.futures.Future is expected, got {future!r}'
418417
if loop is None:
419-
loop = events._get_event_loop()
418+
loop = events.get_running_loop()
420419
new_future = loop.create_future()
421420
_chain_future(future, new_future)
422421
return new_future

Lib/asyncio/streams.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ class FlowControlMixin(protocols.Protocol):
125125

126126
def __init__(self, loop=None):
127127
if loop is None:
128-
self._loop = events._get_event_loop(stacklevel=4)
129-
else:
130-
self._loop = loop
128+
loop = events.get_running_loop()
129+
self._loop = loop
131130
self._paused = False
132131
self._drain_waiter = None
133132
self._connection_lost = False
@@ -406,9 +405,8 @@ def __init__(self, limit=_DEFAULT_LIMIT, loop=None):
406405

407406
self._limit = limit
408407
if loop is None:
409-
self._loop = events._get_event_loop()
410-
else:
411-
self._loop = loop
408+
loop = events.get_running_loop()
409+
self._loop = loop
412410
self._buffer = bytearray()
413411
self._eof = False # Whether we're done.
414412
self._waiter = None # A future used by _wait_for_data()

Lib/asyncio/tasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ def as_completed(fs, *, timeout=None):
588588
from .queues import Queue # Import here to avoid circular import problem.
589589
done = Queue()
590590

591-
loop = events._get_event_loop()
591+
loop = events.get_running_loop()
592592
todo = {ensure_future(f, loop=loop) for f in set(fs)}
593593
timeout_handle = None
594594

@@ -674,7 +674,7 @@ def _ensure_future(coro_or_future, *, loop=None):
674674
'is required')
675675

676676
if loop is None:
677-
loop = events._get_event_loop(stacklevel=4)
677+
loop = events.get_running_loop()
678678
try:
679679
return loop.create_task(coro_or_future)
680680
except RuntimeError:
@@ -755,7 +755,7 @@ def gather(*coros_or_futures, return_exceptions=False):
755755
gather won't cancel any other awaitables.
756756
"""
757757
if not coros_or_futures:
758-
loop = events._get_event_loop()
758+
loop = events.get_running_loop()
759759
outer = loop.create_future()
760760
outer.set_result([])
761761
return outer

Lib/test/test_asyncio/test_base_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ async def coro():
747747
def test_env_var_debug(self):
748748
code = '\n'.join((
749749
'import asyncio',
750-
'loop = asyncio.get_event_loop()',
750+
'loop = asyncio.new_event_loop()',
751751
'print(loop.get_debug())'))
752752

753753
# Test with -E to not fail if the unit test was run with

Lib/test/test_asyncio/test_events.py

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,15 +2709,11 @@ def get_event_loop(self):
27092709
asyncio.set_event_loop_policy(Policy())
27102710
loop = asyncio.new_event_loop()
27112711

2712-
with self.assertWarns(DeprecationWarning) as cm:
2713-
with self.assertRaises(TestError):
2714-
asyncio.get_event_loop()
2715-
self.assertEqual(cm.filename, __file__)
2712+
with self.assertRaisesRegex(RuntimeError, 'no running'):
2713+
asyncio.get_event_loop()
27162714
asyncio.set_event_loop(None)
2717-
with self.assertWarns(DeprecationWarning) as cm:
2718-
with self.assertRaises(TestError):
2719-
asyncio.get_event_loop()
2720-
self.assertEqual(cm.filename, __file__)
2715+
with self.assertRaisesRegex(RuntimeError, 'no running'):
2716+
asyncio.get_event_loop()
27212717

27222718
with self.assertRaisesRegex(RuntimeError, 'no running'):
27232719
asyncio.get_running_loop()
@@ -2731,16 +2727,11 @@ async def func():
27312727
loop.run_until_complete(func())
27322728

27332729
asyncio.set_event_loop(loop)
2734-
with self.assertWarns(DeprecationWarning) as cm:
2735-
with self.assertRaises(TestError):
2736-
asyncio.get_event_loop()
2737-
self.assertEqual(cm.filename, __file__)
2738-
2730+
with self.assertRaisesRegex(RuntimeError, 'no running'):
2731+
asyncio.get_event_loop()
27392732
asyncio.set_event_loop(None)
2740-
with self.assertWarns(DeprecationWarning) as cm:
2741-
with self.assertRaises(TestError):
2742-
asyncio.get_event_loop()
2743-
self.assertEqual(cm.filename, __file__)
2733+
with self.assertRaisesRegex(RuntimeError, 'no running'):
2734+
asyncio.get_event_loop()
27442735

27452736
finally:
27462737
asyncio.set_event_loop_policy(old_policy)
@@ -2759,15 +2750,11 @@ def test_get_event_loop_returns_running_loop2(self):
27592750
loop = asyncio.new_event_loop()
27602751
self.addCleanup(loop.close)
27612752

2762-
with self.assertWarns(DeprecationWarning) as cm:
2763-
loop2 = asyncio.get_event_loop()
2764-
self.addCleanup(loop2.close)
2765-
self.assertEqual(cm.filename, __file__)
2753+
with self.assertRaisesRegex(RuntimeError, 'no running'):
2754+
asyncio.get_event_loop()
27662755
asyncio.set_event_loop(None)
2767-
with self.assertWarns(DeprecationWarning) as cm:
2768-
with self.assertRaisesRegex(RuntimeError, 'no current'):
2769-
asyncio.get_event_loop()
2770-
self.assertEqual(cm.filename, __file__)
2756+
with self.assertRaisesRegex(RuntimeError, 'no running'):
2757+
asyncio.get_event_loop()
27712758

27722759
with self.assertRaisesRegex(RuntimeError, 'no running'):
27732760
asyncio.get_running_loop()
@@ -2781,15 +2768,11 @@ async def func():
27812768
loop.run_until_complete(func())
27822769

27832770
asyncio.set_event_loop(loop)
2784-
with self.assertWarns(DeprecationWarning) as cm:
2771+
with self.assertRaisesRegex(RuntimeError, 'no running'):
27852772
self.assertIs(asyncio.get_event_loop(), loop)
2786-
self.assertEqual(cm.filename, __file__)
2787-
27882773
asyncio.set_event_loop(None)
2789-
with self.assertWarns(DeprecationWarning) as cm:
2790-
with self.assertRaisesRegex(RuntimeError, 'no current'):
2791-
asyncio.get_event_loop()
2792-
self.assertEqual(cm.filename, __file__)
2774+
with self.assertRaisesRegex(RuntimeError, 'no running'):
2775+
asyncio.get_event_loop()
27932776

27942777
finally:
27952778
asyncio.set_event_loop_policy(old_policy)
@@ -2807,7 +2790,7 @@ class TestPyGetEventLoop(GetEventLoopTestsMixin, unittest.TestCase):
28072790
_get_running_loop_impl = events._py__get_running_loop
28082791
_set_running_loop_impl = events._py__set_running_loop
28092792
get_running_loop_impl = events._py_get_running_loop
2810-
get_event_loop_impl = events._py_get_event_loop
2793+
get_event_loop_impl = events._py_get_running_loop
28112794

28122795

28132796
try:
@@ -2821,7 +2804,7 @@ class TestCGetEventLoop(GetEventLoopTestsMixin, unittest.TestCase):
28212804
_get_running_loop_impl = events._c__get_running_loop
28222805
_set_running_loop_impl = events._c__set_running_loop
28232806
get_running_loop_impl = events._c_get_running_loop
2824-
get_event_loop_impl = events._c_get_event_loop
2807+
get_event_loop_impl = events._c_get_running_loop
28252808

28262809

28272810
class TestServer(unittest.TestCase):

0 commit comments

Comments
 (0)