]> granicus.if.org Git - python/commitdiff
bpo-32596: Lazy import concurrent.futures.process and thread (GH-5241)
authorINADA Naoki <methane@users.noreply.github.com>
Sat, 20 Jan 2018 00:54:42 +0000 (09:54 +0900)
committerGitHub <noreply@github.com>
Sat, 20 Jan 2018 00:54:42 +0000 (09:54 +0900)
Lib/concurrent/futures/__init__.py
Misc/NEWS.d/next/Library/2018-01-19-19-57-45.bpo-32596.4aVIie.rst [new file with mode: 0644]

index ba8de16390509338a3e54551b7340fa3a12096c5..72aca818d3ef00efea0ac2e448225ccd1d4e88c3 100644 (file)
@@ -15,5 +15,36 @@ from concurrent.futures._base import (FIRST_COMPLETED,
                                       Executor,
                                       wait,
                                       as_completed)
-from concurrent.futures.process import ProcessPoolExecutor
-from concurrent.futures.thread import ThreadPoolExecutor
+
+__all__ = (
+    'FIRST_COMPLETED',
+    'FIRST_EXCEPTION',
+    'ALL_COMPLETED',
+    'CancelledError',
+    'TimeoutError',
+    'BrokenExecutor',
+    'Future',
+    'Executor',
+    'wait',
+    'as_completed',
+    'ProcessPoolExecutor',
+    'ThreadPoolExecutor',
+)
+
+
+def __dir__():
+    return __all__ + ('__author__', '__doc__')
+
+
+def __getattr__(name):
+    global ProcessPoolExecutor, ThreadPoolExecutor
+
+    if name == 'ProcessPoolExecutor':
+        from .process import ProcessPoolExecutor
+        return ProcessPoolExecutor
+
+    if name == 'ThreadPoolExecutor':
+        from .thread import ThreadPoolExecutor
+        return ThreadPoolExecutor
+
+    raise AttributeError(f"module {__name__} has no attribute {name}")
diff --git a/Misc/NEWS.d/next/Library/2018-01-19-19-57-45.bpo-32596.4aVIie.rst b/Misc/NEWS.d/next/Library/2018-01-19-19-57-45.bpo-32596.4aVIie.rst
new file mode 100644 (file)
index 0000000..a90f7d1
--- /dev/null
@@ -0,0 +1,4 @@
+``concurrent.futures`` imports ``ThreadPoolExecutor`` and
+``ProcessPoolExecutor`` lazily (using :pep:`562`).
+It makes ``import asyncio`` about 15% faster because asyncio
+uses only ``ThreadPoolExecutor`` by default.