手把手带你了解python多进程,多线程

这篇文章主要介绍了python多线程与多进程及其区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

目录

多进程

多线程

线程安全

高并发拷贝(多进程,多线程)

总结

说明

相应的学习视频见链接,本文只对重点进行总结。

多进程

重点(只要看下面代码的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:UsersAdministratorDesktop注意力' dest_dir=r'C:UsersAdministratorDesktoptest' start = time.time() try: os.mkdir(dest_dir) except: print("目标文件已存在") file_list上一篇:昨晚我用python帮隔壁小姐姐P证件照然后发现下一篇:python接口,继承,重载运算符详解 热门搜索:

python多线程 

python多线程编程 

多进程 

多进程编程 

多线程 

相关文章

手把手带你了解python多进程,多线程

2021-09-10阅读(5359)评论(0)推荐()

这篇文章主要介绍了python多线程与多进程及其区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

python 多进程和多线程使用详解

2021-10-09阅读(3199)评论(0)推荐()

这篇文章主要介绍了python 多进程和多线程使用详解,帮助大家更好得理解和学习使用python,感兴趣的朋友可以了解下

Python多线程处理实例详解【单进程/多进程】

2021-10-09阅读(5763)评论(0)推荐()

这篇文章主要介绍了Python多线程处理,结合实例形式总结分析了Python单进程、多进程、多线程等相关操作技巧与注意事项,需要的朋友可以参考下

Python控制多进程与多线程并发数总结

2021-09-10阅读(9832)评论(0)推荐()

本篇文章主要介绍了Python控制多进程与多线程并发数,详细讲诉了进程和线程的区别,并介绍了处理方法,有需要的朋友可以了解一下。

Python实现的自定义多线程多进程类示例

2021-11-04阅读(9448)评论(0)推荐()

这篇文章主要介绍了Python实现的自定义多线程多进程类,结合实例形式分析了Python多线程多进程的相关调用与使用操作技巧,需要的朋友可以参考下

Python实现的多进程和多线程功能示例

2021-10-09阅读(9502)评论(0)推荐()

这篇文章主要介绍了Python实现的多进程和多线程功能,结合实例形式分析了Python多线程与多进程实现分布式系统功能相关操作技巧,需要的朋友可以参考下

python多进程和多线程究竟谁更快(详解)

2021-10-17阅读(5379)评论(0)推荐()

下面小编就为大家带来一篇python多进程和多线程究竟谁更快(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

取消

有人回复时邮件通知我

提交评论

© 2021 编程之家 

工信部备案号:琼ICP备2022000316号

相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...