import threading,time,concurrent.futures
from concurrent.futures import *
import matplotlib.pyplot as plt
def codemaker(func):
def wrapper(*args,**kwargs):
start = time.time()
func(*args,**kwargs)
end = time.time()
print(args)
plt.plot([start,end],[kwargs["trial"]]*2)
plt.xlabel('time')
plt.ylabel('trial')
#print(start,end-start)
return wrapper
plt.yticks([0,1,2,3,4])
@codemaker
def io_heavy(text,trial=0):
with open('output.txt', 'wt', encoding='utf-8') as f:f.write(text) #None is result of time.sleep() yielded by generator
list(ThreadPoolExecutor(max_workers=3).map(io_heavy,["ss",'dd','ff','gg'],[{"trial":i} for i in range(4)]))
#io_heavy("text",trial=0)
#io_heavy("text",trial=1)
#io_heavy("text",trial=2)
plt.show()
输出显示有关args的信息 {'trial':0},0],'gg' 如此多次
和大约{kwargs {}次。 我怎样才能传递给ThreadPoolExecutor呢?
文档站点docs.python.org/3/library/concurrent.futures.html 仅显示
map(func,* iterables,timeout = None,chunksize = 1)