文章目录
1 概述
1.1 进程 VS 线程
举例说明:
进程:QQ.exe
线程:支撑 QQ.exe 运行的各个程序
1.2 优缺点
优点 | 缺点 | |
---|---|---|
进程 | 可用多核 CPU | 资源开销大 |
线程 | 资源开销小 | 只能单核 CPU |
2 进程
2.1 三个步骤
# 1.导入进程包
import ultiprocessing
# 2.通过进程类创建进程对象,参数名有
# target: 执行的目标任务名(函数名、方法名)
# name : 进程名,一般不用设置
# group : 进程组,一般不用设置
进程对象 = ultiprocessing.Process(target=任务名)
# 3.启动进程
进程对象.start()
2.2 多进程
import multiprocessing
import time
# 唱歌
def sing():
for i in range(3):
print("唱歌...")
time.sleep(1)
# 跳舞
def dance():
for i in range(3):
print("跳舞...")
time.sleep(1)
if __name__ == '__main__':
sing_process = multiprocessing.Process(target=sing)
dance_process = multiprocessing.Process(target=dance)
sing_process.start()
dance_process.start()
2.3 带参数
参数 | 描述 |
---|---|
元组 args | 参数有顺序要求 |
字典 kwargs | 参数无顺序要求 |
import multiprocessing
import time
# 唱歌
def sing(num):
for i in range(num):
print("唱歌...")
time.sleep(1)
# 跳舞
def dance(num):
for i in range(num):
print("跳舞...")
time.sleep(1)
if __name__ == '__main__':
sing_process = multiprocessing.Process(target=sing, args=(3,))
dance_process = multiprocessing.Process(target=dance, kwargs={"num": 3})
sing_process.start()
dance_process.start()
2.3.1 元组参数 args
sing_process = multiprocessing.Process(target=sing, args=(3,))
sing_process.start()
2.3.2 字典参数 kwargs
sing_process = multiprocessing.Process(target=dance, kwargs={"num": 3})
sing_process.start()
2.4 获取进程编号
import os
print(os.getpid()) # 当前进程ID
print(os.getppid()) # 当前进程的父ID
2.5 设置进程守护
import multiprocessing
import time
# 工作
def work():
for i in range(10):
print("工作中...")
time.sleep(0.5)
if __name__ == '__main__':
work_process = multiprocessing.Process(target=work)
# 设置守护进程
work_process.daemon = True
work_process.start()
time.sleep(1)
print("主进程执行已完成!")
设置守护进程的前后对比图:
3 线程
3.1 三个步骤
# 1.导入线程模块
import threading
# 2.通过线程类创建线程对象,参数名有
# target: 执行的目标任务名(函数名、方法名)
# name : 线程名,一般不用设置
# group : 线程组,一般不用设置
线程对象 = threading.Thread(target=任务名)
# 3.启动线程执行任务
线程对象.start()
3.2 多线程
import threading
import time
# 唱歌
def sing():
for i in range(3):
print("唱歌...")
time.sleep(1)
# 跳舞
def dance():
for i in range(3):
print("跳舞...")
time.sleep(1)
if __name__ == '__main__':
sing_process = threading.Thread(target=sing)
dance_process = threading.Thread(target=dance)
sing_process.start()
dance_process.start()
3.3 带参数
- 同进程
import threading
import time
# 唱歌
def sing(num):
for i in range(num):
print("唱歌...")
time.sleep(1)
# 跳舞
def dance(num):
for i in range(num):
print("跳舞...")
time.sleep(1)
if __name__ == '__main__':
sing_process = threading.Thread(target=sing, args=(3,))
dance_process = threading.Thread(target=dance, kwargs={"num": 3})
sing_process.start()
dance_process.start()
2.3.1 元组参数 args
sing_process = multiprocessing.Thread(target=sing, args=(3,))
sing_process.start()
2.3.2 字典参数 kwargs
sing_process = multiprocessing.Thread(target=dance, kwargs={"num": 3})
sing_process.start()
2.4 获取线程编号
import threading
def work():
print(f"线程ID为: {threading.current_thread().ident}")
if __name__ == '__main__':
work_thread = threading.Thread(target=work)
work_thread.start()
2.5 设置线程守护
- 同 进程
import threading
import time
# 工作
def work():
for i in range(10):
print("工作中...")
time.sleep(0.5)
if __name__ == '__main__':
work_thread = threading.Thread(target=work)
# 设置守护线程
work_thread.setDaemon(True)
work_thread.start()
time.sleep(1)
print("主进程执行已完成!")