From 0fa144f0125ba34a5e94d6d59490d14ecbf47d49 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 19 Jan 2018 19:54:22 +0900 Subject: [PATCH 1/2] bpo-32596: Lazy import concurrent.futures.process and thread Using module __getattr__ (PEP 562) --- Lib/concurrent/futures/__init__.py | 35 ++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/Lib/concurrent/futures/__init__.py b/Lib/concurrent/futures/__init__.py index ba8de1639050933..72aca818d3ef00e 100644 --- a/Lib/concurrent/futures/__init__.py +++ b/Lib/concurrent/futures/__init__.py @@ -15,5 +15,36 @@ 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}") From 38d55acdcd537a4239ea01a1b851b50badcab843 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 19 Jan 2018 19:57:47 +0900 Subject: [PATCH 2/2] Add NEWS entry --- .../next/Library/2018-01-19-19-57-45.bpo-32596.4aVIie.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-01-19-19-57-45.bpo-32596.4aVIie.rst 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 index 000000000000000..a90f7d176539143 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-01-19-19-57-45.bpo-32596.4aVIie.rst @@ -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.