<reponame>relax-space/thread-first
'''
说明: 多线程并发执行任务,比单线程要节约时间
'''
import time
from queue import Queue
from threading import Thread


def req1(param):
    time.sleep(1)
    return param


def main1():
    return [req1(1), req1(2)]


def req2(param, res_value: Queue):
    time.sleep(1)
    res_value.put(param)


def main2():
    res_value = Queue()
    tasks = [Thread(target=req2, args=(1, res_value)),
             Thread(target=req2, args=(2, res_value))]
    for i in tasks:
        i.start()

    for i in tasks:
        i.join()

    return [res_value.get() for i in tasks]


if __name__ == '__main__':
    t1 = time.time()
    res1 = main1()
    t2 = time.time()
    res2 = main2()
    t3 = time.time()
    print(f'main1结果{res1}\t时间{round(t2-t1,1)}')
    print(f'main2结果{res2}\t时间{round(t3-t2,1)}')

'''
输出:
    main1结果[1, 2] 时间2.0
    main2结果[2, 1] 时间1.0
'''

'''
说明: 多线程并发执行任务,比单线程要节约时间
'''
import time
from queue import Queue
from threading import Thread


def req1(param):
    time.sleep(1)
    return param


def main1():
    return [req1(1), req1(2)]


def req2(param, res_value: Queue):
    time.sleep(1)
    res_value.put(param)


def main2():
    res_value = Queue()
    tasks = [Thread(target=req2, args=(1, res_value)),
             Thread(target=req2, args=(2, res_value))]
    for i in tasks:
        i.start()

    for i in tasks:
        i.join()

    return [res_value.get() for i in tasks]


if __name__ == '__main__':
    t1 = time.time()
    res1 = main1()
    t2 = time.time()
    res2 = main2()
    t3 = time.time()
    print(f'main1结果{res1}\t时间{round(t2-t1,1)}')
    print(f'main2结果{res2}\t时间{round(t3-t2,1)}')

'''
输出:
    main1结果[1, 2] 时间2.0
    main2结果[2, 1] 时间1.0
'''
说明: 多线程并发执行任务,比单线程要节约时间 输出:     main1结果[1, 2] 时间2.0     main2结果[2, 1] 时间1.0