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

Skip to content

Commit 6690bb9

Browse files
authored
bpo-32596: Lazy import concurrent.futures.process and thread (GH-5241)
1 parent 338cd83 commit 6690bb9

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

Lib/concurrent/futures/__init__.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,36 @@
1515
Executor,
1616
wait,
1717
as_completed)
18-
from concurrent.futures.process import ProcessPoolExecutor
19-
from concurrent.futures.thread import ThreadPoolExecutor
18+
19+
__all__ = (
20+
'FIRST_COMPLETED',
21+
'FIRST_EXCEPTION',
22+
'ALL_COMPLETED',
23+
'CancelledError',
24+
'TimeoutError',
25+
'BrokenExecutor',
26+
'Future',
27+
'Executor',
28+
'wait',
29+
'as_completed',
30+
'ProcessPoolExecutor',
31+
'ThreadPoolExecutor',
32+
)
33+
34+
35+
def __dir__():
36+
return __all__ + ('__author__', '__doc__')
37+
38+
39+
def __getattr__(name):
40+
global ProcessPoolExecutor, ThreadPoolExecutor
41+
42+
if name == 'ProcessPoolExecutor':
43+
from .process import ProcessPoolExecutor
44+
return ProcessPoolExecutor
45+
46+
if name == 'ThreadPoolExecutor':
47+
from .thread import ThreadPoolExecutor
48+
return ThreadPoolExecutor
49+
50+
raise AttributeError(f"module {__name__} has no attribute {name}")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``concurrent.futures`` imports ``ThreadPoolExecutor`` and
2+
``ProcessPoolExecutor`` lazily (using :pep:`562`).
3+
It makes ``import asyncio`` about 15% faster because asyncio
4+
uses only ``ThreadPoolExecutor`` by default.

0 commit comments

Comments
 (0)