一、多线程
import random
import sys
import time
import threading
def func(count):
time_out = random.randint(2, 5)
sys.stdout.write(f'count={count}, 线程名称是: {threading.current_thread().name}, 此函数线程运行{time_out}秒\n')
time.sleep(time_out) # 强制等待2-5秒
return count
class MyThread(threading.Thread):
def __init__(self, args):
super(MyThread, self).__init__()
self.args = args
def run(self):
func(self.args)
if __name__ == '__main__':
start_time = time.time()
task = []
for i in range(10):
# ---------------1、直接调用---------------
# t = threading.Thread(target=func, args=(i,)) # args函数对应的参数,是元组
# ---------------2、继承threading.Thread类重写run()方法。-----------------
t = MyThread(i)
t.start()
task.append(t)
for t in task:
t.join()
print(f'耗时: {time.time() - start_time}, 线程名称是: {threading.current_thread().name}')
二、线程池
import random
import sys
import time
import threading
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
def func(count):
time_out = random.randint(2, 5)
sys.stdout.write(f'count={count}, 线程名称是: {threading.current_thread().name}, 此函数线程运行{time_out}秒\n')
time.sleep(time_out) # 强制等待2-5秒
return count
if __name__ == '__main__':
start_time = time.time()
pool = ThreadPoolExecutor(10)
# ---------------------------1、参数确定的情况下,直接加任务-----------------------------------
# task = pool.map(func, range(10))
# for i in task:
# data = i
# print(f'data={data}, 当前线程名称是: {threading.current_thread().name}\n')
# ---------------------------2、逐步遍历添加任务--------------------------------------------
task = []
for i in range(10):
task.append(pool.submit(func, i))
# 等待线程池中任务执行完后再执行主线程
# for i in task:
for i in as_completed(task):
# as_completed()先执行完先返回,直接task就是按加入顺序返回
data = i.result()
print(f'data={data}, 当前线程名称是: {threading.current_thread().name}\n')
# --------------------------------------------------------------------------------------
print(f'时间间隔是: {time.time() - start_time}, 线程名称是: {threading.current_thread().name}')
三、多进程(和多线程一毛一样)
import os
import random
import sys
import time
from multiprocessing import Process
def func(count):
time_out = random.randint(2, 5)
sys.stdout.write(f'count={count}, 进程名称是: 进程-{os.getpid()}, 此函数线程运行{time_out}秒\n')
time.sleep(time_out) # 强制等待2-5秒
return count
class MyProcess(Process):
def __init__(self, arg):
super(MyProcess, self).__init__()
self.arg = arg
def run(self):
func(self.arg)
if __name__ == '__main__':
start_time = time.time()
task = []
for i in range(10):
# p = Process(target=func, args=(i,))
p = MyProcess(i)
p.start()
task.append(p)
for p in task:
p.join()
print(f'耗时: {time.time() - start_time}, 进程名称是: 进程-{os.getpid()}')
四、进程池
import os
import random
import sys
import time
from multiprocessing.pool import Pool
def func(count):
time_out = random.randint(2, 5)
sys.stdout.write(f'count={count}, 进程名称是: 进程-{os.getpid()}, 此函数线程运行{time_out}秒\n')
time.sleep(time_out) # 强制等待2-5秒
return count
if __name__ == '__main__':
start_time = time.time()
p = Pool(10) # Pool的默认大小是你所用的电脑CPU的核数,CPU核数可通过os.cpu_count()获得
# ---------------------1、参数确定的情况下,直接加任务---------------------------
# task = p.map(func, range(10))
# ----------------------2、逐步遍历添加任务--------------------------
task = []
for i in range(10):
task.append(p.apply_async(func, args=(i,))) # apply_async 异步
p.close() # 在调用p.join()之前需要先调用p.close()
p.join()
# ------------------------------------------------
for t in task:
data = t.get() # 同步就不需要.get()了
print(f'data={data}, 当前线程名称是: 进程-{os.getpid()}')
print(f'耗时: {time.time() - start_time}, 进程名称是: 进程-{os.getpid()}')