获取线程的执行结果

2021-03-08 23:29

阅读:538

标签:app   wrapper   append   运行时间   out   name   display   rgba   thread   

code
import threading
from queue import Queue
import time
 
def timeit(f):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        res = f(*args, **kwargs)
        end_time = time.time()
        print("%s函数运行时间:%.8f" % (f.__name__, end_time - start_time))
        return res
    return wrapper
 
def job(li, queue):
    queue.put(sum(li))
 
@timeit
def use_thread():
    q = Queue()
    lis = [range(5), range(2,10), range(1000, 20000), range(3000, 10000)]
    threads = []
    for li in lis:
        t = threading.Thread(target=job, args=(li, q))
        t.start()
        threads.append(t)
    [thread.join() for thread in threads]
    results  = [q.get() for li in lis]
    print(results)
 
if __name__ == "__main__":
    use_thread()
outputs
macname@MacdeMacBook-Pro py % python3 cccccc.py
[10, 44, 199490500, 45496500]
use_thread函数运行时间:0.00095630
macname@MacdeMacBook-Pro py %
 
 
 
 
 
 
 
 
 
 
 
 

获取线程的执行结果

标签:app   wrapper   append   运行时间   out   name   display   rgba   thread   

原文地址:https://www.cnblogs.com/sea-stream/p/14192614.html


评论


亲,登录后才可以留言!