diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 5dd258df3127a67..8b41772627076f9 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -881,6 +881,12 @@ Server The server is closed asynchronously, use the :meth:`wait_closed` coroutine to wait until the server is closed. + .. method:: get_loop() + + Gives the event loop associated with the server object. + + .. versionadded:: 3.7 + .. coroutinemethod:: wait_closed() Wait until the :meth:`close` method completes. diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 96cc4f02588ac8f..edf8ccd2d98e711 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -179,6 +179,9 @@ def close(self): if self._active_count == 0: self._wakeup() + def get_loop(self): + return self._loop + def _wakeup(self): waiters = self._waiters self._waiters = None diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 3a5dbadbb105bfc..5702e1eb41ec7bf 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -155,6 +155,10 @@ async def wait_closed(self): """Coroutine to wait until service is closed.""" return NotImplemented + def get_loop(self): + """ Get the event loop the Server object is attached to.""" + return NotImplemented + class AbstractEventLoop: """Abstract event loop.""" diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index 79e8d79e6b14b23..bf6ac1c735dae39 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2865,6 +2865,19 @@ class TestCGetEventLoop(GetEventLoopTestsMixin, unittest.TestCase): get_running_loop_impl = events._c_get_running_loop get_event_loop_impl = events._c_get_event_loop +class TestServer(unittest.TestCase): + + def test_get_loop(self): + loop = asyncio.new_event_loop() + proto = MyProto(loop) + server = loop.run_until_complete(loop.create_server(lambda: proto, '0.0.0.0', 0)) + self.assertEqual(server.get_loop(), loop) + loop.close() + +class TestAbstractServer(unittest.TestCase): + + def test_get_loop(self): + self.assertEqual(events.AbstractServer().get_loop(), NotImplemented) if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Documentation/2017-12-24-17-29-37.bpo-32418.eZe-ID.rst b/Misc/NEWS.d/next/Documentation/2017-12-24-17-29-37.bpo-32418.eZe-ID.rst new file mode 100644 index 000000000000000..9441b74ef844c4f --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2017-12-24-17-29-37.bpo-32418.eZe-ID.rst @@ -0,0 +1 @@ +Add get_loop() method to Server and AbstractServer classes.