]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-99388: add `loop_factory` parameter to `asyncio.run` (#99462)
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Mon, 14 Nov 2022 18:18:51 +0000 (23:48 +0530)
committerGitHub <noreply@github.com>
Mon, 14 Nov 2022 18:18:51 +0000 (10:18 -0800)
Doc/library/asyncio-runner.rst
Doc/whatsnew/3.12.rst
Lib/asyncio/runners.py
Lib/test/test_asyncio/test_runners.py
Misc/NEWS.d/next/Library/2022-11-14-08-21-56.gh-issue-99388.UWSlwp.rst [new file with mode: 0644]

index c43d664eba717f209c1b561b5cb4a2e586cb3be0..b68b2570ef071e14b2a0084abacc3f3d2e361e3a 100644 (file)
@@ -22,7 +22,7 @@ to simplify async code usage for common wide-spread scenarios.
 Running an asyncio Program
 ==========================
 
-.. function:: run(coro, *, debug=None)
+.. function:: run(coro, *, debug=None, loop_factory=None)
 
    Execute the :term:`coroutine` *coro* and return the result.
 
@@ -37,9 +37,11 @@ Running an asyncio Program
    debug mode explicitly. ``None`` is used to respect the global
    :ref:`asyncio-debug-mode` settings.
 
-   This function always creates a new event loop and closes it at
-   the end.  It should be used as a main entry point for asyncio
-   programs, and should ideally only be called once.
+   If *loop_factory* is not ``None``, it is used to create a new event loop;
+   otherwise :func:`asyncio.new_event_loop` is used. The loop is closed at the end.
+   This function should be used as a main entry point for asyncio programs,
+   and should ideally only be called once. It is recommended to use
+   *loop_factory* to configure the event loop instead of policies.
 
    The executor is given a timeout duration of 5 minutes to shutdown.
    If the executor hasn't finished within that duration, a warning is
@@ -62,6 +64,10 @@ Running an asyncio Program
 
       *debug* is ``None`` by default to respect the global debug mode settings.
 
+   .. versionchanged:: 3.12
+
+      Added *loop_factory* parameter.
+
 
 Runner context manager
 ======================
index d30e4195bc2417e05d663bdeab0dff66a7def59f..bb08e4455ee1785d1c675d56bcb606a5033c33d5 100644 (file)
@@ -217,6 +217,11 @@ asyncio
   and will be removed in Python 3.14.
   (Contributed by Kumar Aditya in :gh:`94597`.)
 
+* Add *loop_factory* parameter to :func:`asyncio.run` to allow specifying
+  a custom event loop factory.
+  (Contributed by Kumar Aditya in :gh:`99388`.)
+
+
 pathlib
 -------
 
index b1c4dbd76197214946f122d2959dc8b21a517040..1b89236599aad7052f7e13b2e826a5d1d13875de 100644 (file)
@@ -157,7 +157,7 @@ class Runner:
         raise KeyboardInterrupt()
 
 
-def run(main, *, debug=None):
+def run(main, *, debug=None, loop_factory=None):
     """Execute the coroutine and return the result.
 
     This function runs the passed coroutine, taking care of
@@ -190,7 +190,7 @@ def run(main, *, debug=None):
         raise RuntimeError(
             "asyncio.run() cannot be called from a running event loop")
 
-    with Runner(debug=debug) as runner:
+    with Runner(debug=debug, loop_factory=loop_factory) as runner:
         return runner.run(main)
 
 
index 1308b7e2ba4f82836998abeb72a2593d52dcb4ac..811cf8b72488b8ca113320e2b1e9f12e08139c41 100644 (file)
@@ -257,6 +257,16 @@ class RunTests(BaseTest):
         with self.assertRaises(asyncio.CancelledError):
             asyncio.run(main())
 
+    def test_asyncio_run_loop_factory(self):
+        factory = mock.Mock()
+        loop = factory.return_value = self.new_loop()
+
+        async def main():
+            self.assertEqual(asyncio.get_running_loop(), loop)
+
+        asyncio.run(main(), loop_factory=factory)
+        factory.assert_called_once_with()
+
 
 class RunnerTests(BaseTest):
 
diff --git a/Misc/NEWS.d/next/Library/2022-11-14-08-21-56.gh-issue-99388.UWSlwp.rst b/Misc/NEWS.d/next/Library/2022-11-14-08-21-56.gh-issue-99388.UWSlwp.rst
new file mode 100644 (file)
index 0000000..f35799d
--- /dev/null
@@ -0,0 +1,2 @@
+Add *loop_factory* parameter to :func:`asyncio.run` to allow specifying a custom event loop factory.\r
+Patch by Kumar Aditya.