手把手带你了解python多进程,多线程
作者:Mr DaYang 时间:2021-07-16 02:08:42
说明
相应的学习视频见链接,本文只对重点进行总结。
多进程
重点(只要看下面代码的main函数即可)
1.创建
2.如何开守护进程
3.多进程,开销大,用for循环调用多个进程时,后台cpu一下就上去了
import time
import multiprocessing
import os
def dance(who,num):
print("dance父进程:{}".format(os.getppid()))
for i in range(1,num+1):
print("进行编号:{}————{}跳舞。。。{}".format(os.getpid(),who,i))
time.sleep(0.5)
def sing(num):
print("sing父进程:{}".format(os.getppid()))
for i in range(1,num+1):
print("进行编号:{}----唱歌。。。{}".format(os.getpid(),i))
time.sleep(0.5)
def work():
for i in range(10):
print("工作中。。。")
time.sleep(0.2)
if __name__ == '__main__':
# print("main主进程{}".format(os.getpid()))
start= time.time()
#1 进程的创建与启动
# # 1.1创建进程对象,注意dance不能加括号
# # dance_process = multiprocessing.Process(target=dance)#1.无参数
# dance_process=multiprocessing.Process(target=dance,args=("lin",3))#2.以args=元祖方式
# sing_process = multiprocessing.Process(target=sing,kwargs={"num":3})#3.以kwargs={}字典方式
# # 1.2启动进程
# dance_process.start()
# sing_process.start()
#2.默认-主进程和子进程是分开的,主进程只要1s就可以完成,子进程要2s,主进程会等所有子进程执行完,再退出
# 2.1子守护主进程,当主一但完成,子就断开(如qq一关闭,所有聊天窗口就没了).daemon=True
work_process = multiprocessing.Process(target=work,daemon=True)
work_process.start()
time.sleep(1)
print("主进程完成了!")#主进程和子进程是分开的,主进程只要1s就可以完成,子进程要2s,主进程会等所有子进程执行完,再退出
print("main主进程花费时长:",time.time()-start)
#
多线程
重点
1.创建
2.守护线程
3.线程安全问题(多人抢票,会抢到同一张)
import time
import os
import threading
def dance(num):
for i in range(num):
print("进程编号:{},线程编号:{}————跳舞。。。".format(os.getpid(),threading.current_thread()))
time.sleep(1)
def sing(count):
for i in range(count):
print("进程编号:{},线程编号:{}----唱歌。。。".format(os.getpid(),threading.current_thread()))
time.sleep(1)
def task():
time.sleep(1)
thread=threading.current_thread()
print(thread)
if __name__ == '__main__':
# start=time.time()
# # sing_thread =threading.Thread(target=dance,args=(3,),daemon=True)#设置成守护主线程
# sing_thread = threading.Thread(target=dance, args=(3,))
# dance_thread = threading.Thread(target=sing,kwargs={"count":3})
#
# sing_thread.start()
# dance_thread.start()
#
# time.sleep(1)
# print("进程编号:{}主线程结束...用时{}".format(os.getpid(),(time.time()-start)))
for i in range(10):#多线程之间执行是无序的,由cpu调度
sub_thread = threading.Thread(target=task)
sub_thread.start()
线程安全
由于线程直接是无序进行的,且他们共享同一个进程的全部资源,所以会产生线程安全问题(比如多人在线抢票,买到同一张)
#下面代码在没有lock锁时,会卖出0票,加上lock就正常
import threading
import time
lock =threading.Lock()
class Sum_tickets:
def __init__(self,tickets):
self.tickets=tickets
def window(sum_tickets):
while True:
with lock:
if sum_tickets.tickets>0:
time.sleep(0.2)
print(threading.current_thread().name,"取票{}".format(sum_tickets.tickets))
sum_tickets.tickets-=1
else:
break
if __name__ == '__main__':
sum_tickets=Sum_tickets(10)
sub_thread1 = threading.Thread(name="窗口1",target=window,args=(sum_tickets,))
sub_thread2 = threading.Thread(name="窗口2",target=window,args=(sum_tickets,))
sub_thread1.start()
sub_thread2.start()
高并发拷贝(多进程,多线程)
import os
import multiprocessing
import threading
import time
def copy_file(file_name,source_dir,dest_dir):
source_path = source_dir+"/"+file_name
dest_path =dest_dir+"/"+file_name
print("当前进程为:{}".format(os.getpid()))
with open(source_path,"rb") as source_file:
with open(dest_path,"wb") as dest_file:
while True:
data=source_file.read(1024)
if data:
dest_file.write(data)
else:
break
pass
if __name__ == '__main__':
source_dir=r'C:\Users\Administrator\Desktop\注意力'
dest_dir=r'C:\Users\Administrator\Desktop\test'
start = time.time()
try:
os.mkdir(dest_dir)
except:
print("目标文件已存在")
file_list =os.listdir(source_dir)
count=0
#1多进程
for file_name in file_list:
count+=1
print(count)
sub_processor=multiprocessing.Process(target=copy_file,
args=(file_name,source_dir,dest_dir))
sub_processor.start()
# time.sleep(20)
print(time.time()-start)
#这里有主进程和子进程,通过打印可以看出,主进程在创建1,2,3,4,,,21过程中,子进程已有的开始执行,也就是说,每个进程是互不影响的
# 9
# 10
# 11
# 12
# 13
# 当前进程为:2936(当主进程创建第13个时,此时,第一个子进程开始工作)
# 14
# 当前进程为:10120
# 当前进程为:10440
# 15
# 当前进程为:9508
# 2多线程
# for file_name in file_list:
# count += 1
# print(count)
# sub_thread = threading.Thread(target=copy_file,
# args=(file_name, source_dir, dest_dir))
# sub_thread.start()
# # time.sleep(20)
# print(time.time() - start)
来源:https://blog.csdn.net/m0_46204224/article/details/119737937
标签:python,多进程,多线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PyQt5高级界面控件之QTableWidget的具体使用方法
2023-09-05 15:36:41
![](https://img.aspxhome.com/file/2023/0/72240_0s.png)
Python 在 VSCode 中使用 IPython Kernel 的方法详解
2023-07-25 10:10:23
![](https://img.aspxhome.com/file/2023/7/72047_0s.png)
如何实现SQL Server 2005快速Web分页
2009-01-21 14:51:00
python列表详情
2023-12-29 01:59:06
python在非root权限下的安装方法
2023-12-05 20:28:35
JS复制特定内容到粘贴板
2011-04-02 11:09:00
Django卸载之后重新安装的方法
2023-07-23 18:27:58
ADSI+ASP添加IP到IIS禁止访问列表中
2011-04-02 10:42:00
利用python+ffmpeg合并B站视频及格式转换的实例代码
2021-06-09 21:14:00
百度编辑器复制微信图片无法保存
2023-08-14 17:32:46
![](https://img.aspxhome.com/file/202308/1692005789643522s.png)
Python函数定义及传参方式详解(4种)
2022-08-16 14:39:08
python中itertools模块zip_longest函数详解
2023-01-02 09:09:35
javascript new fun的执行过程
2010-08-05 21:23:00
![](https://img.aspxhome.com/file/UploadPic/20108/5/20100805150736846-93s.jpg)
地图可视化神器kepler.gl python接口的使用方法
2022-05-07 00:10:57
![](https://img.aspxhome.com/file/2023/2/95422_0s.png)
python本地文件服务器实例教程
2022-07-31 16:38:17
![](https://img.aspxhome.com/file/2023/7/64607_0s.jpg)
ASP中类的详细介绍(class Property Get、Property Let)
2008-02-20 19:18:00
js实现用div层模拟的小窗口
2007-12-02 15:01:00
![](https://img.aspxhome.com/file/UploadPic/200712/2/2007122151314277s.gif)
详解python logging日志传输
2021-04-20 20:43:08
MSSQL安全设置的具体步骤和方法小结
2012-07-11 15:54:11
php pdo oracle中文乱码的快速解决方法
2023-11-16 22:12:47