from concurrent. futures import ThreadPoolExecutor
import threading
import time
var = 1
local = threading. local( ) ;
def func ( ) :
count = 0
for i in range ( 100 ) :
local. x = var
if testa( ) and testb( ) and testc( ) :
count = count + 1
print ( '线程私有变量:{}' . format ( local. x) )
time. sleep( 1 )
return count
def testa ( ) :
local. x = local. x + 1
return True
def testb ( ) :
local. x = local. x + 2
return True
def testc ( ) :
local. x = local. x + 3
return True
if __name__ == '__main__' :
t = ThreadPoolExecutor( max_workers= 20 )
tasks = [ ]
results = [ ]
for i in range ( 20 ) :
tasks. append( t. submit( func) )
print ( '任务提交完毕' )
for i in range ( len ( tasks) ) :
results. append( tasks[ i] . result( ) )
t. shutdown( )
print ( results)
print ( '主线程运行结束……' )
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7
线程私有变量:7 任务提交完毕
[ 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 ]
主线程运行结束……