python多进程和多线程知识点整理

  介绍

这篇文章主要讲解了“python多进程和多线程知识点整理”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python多进程和多线程知识点整理”吧!

<编辑类="目录">目录 <李>

多进程

<李>

多线程

<李>

线程安全

<李>

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

<强>说明

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

 python多进程和多线程知识点整理

 python多进程和多线程知识点整理

多进程

重点(只要看下面代码的主要函数即可)

1。创建

2。如何开守护进程

3。多进程,开销大,用循环调用多个进程时,后一台cpu下就上去了

import 时间   import 多处理   import 操作系统   def 舞蹈(num):   ,,,print(“舞蹈父进程:{}“.format (os.getppid ()))   ,,,for 小姐:拷贝范围(1,num + 1):   ,,,,,,,印刷(“进行编号:{},{}跳舞…{}“.format (os.getpid (),, i))   ,,,,,,,time . sleep (0.5)   def 唱(num):   ,,,print(“父唱进程:{}“.format (os.getppid ()))   ,,,for 小姐:拷贝范围(1,num + 1):   ,,,,,,,印刷(“进行编号:{}——唱的歌…{}“.format (os.getpid (), i))   ,,,,,,,time . sleep (0.5)   def 工作():   ,,,for 小姐:拷贝范围(10):   ,,,,,,,印刷(“工作中…“)   ,,,,,,,time . sleep (0.2)   if  __name__ ==, & # 39; __main__ # 39;:   ,,,#,打印(“主要主进程{}“.format (os.getpid ()))   开始,,,=,time.time ()   ,,,# 1,进程的创建与启动   ,,,#,#,1.1创建进程对象,注意不跳舞能加括号   ,,,#,#,dance_process =, multiprocessing.Process(目标=跳舞)# 1。无参数   ,,,#,dance_process=multiprocessing.Process(目标=跳舞,arg游戏=(“lin", 3)) # 2。以args=元祖方式   ,,,#,sing_process =, multiprocessing.Process(目标=唱,kwargs={“num": 3}) # 3。以kwargs={}字典方式   ,,,#,#,1.2启动进程   ,,,#,dance_process.start ()   ,,,#,sing_process.start ()   ,,,# 2。默认——主进程和子进程是分开的,主进程只要1 s就可以完成,子进程要2 s,主进程会等所有子进程执行完,再退出   ,,,#,2.1子守护主进程,当主一但完成,子就断开(如qq一关闭,所有聊天窗口就没了).daemon=True   ,,,work_process =, multiprocessing.Process(=工作目标,守护进程=True)   ,,,work_process.start ()   ,,,time . sleep (1)   ,,,print(“主进程完成了!“)#主进程和子进程是分开的,主进程只要1 s就可以完成,子进程要2 s,主进程会等所有子进程执行完,再退出   ,,,print(“主要主进程花费时长:“,time.time()实体法)   ,,,#

多线程

 python多进程和多线程知识点整理”> <br/> </p> <p>重点</p> <p> 1。创建</p> <p> 2。守护线程</p> <p> 3。线程安全问题(多人抢票,会抢到同一张)</p> <pre类= import 时间   import 操作系统   import 线程   def 舞蹈(num):   ,,,for 小姐:拷贝范围(num):   ,,,,,,,印刷(“进程编号:{},线程编号:{}- - - - -跳舞…“.format (os.getpid (), threading.current_thread ()))   ,,,,,,,time . sleep (1)   def 唱(数):   ,,,for 小姐:拷贝范围(数):   ,,,,,,,印刷(“进程编号:{},线程编号:{}——唱的歌…“.format (os.getpid (), threading.current_thread ()))   ,,,,,,,time . sleep (1)   def 任务():   ,,,time . sleep (1)   ,,,线程=threading.current_thread ()   ,,,print(线程)   if  __name__ ==, & # 39; __main__ # 39;:   ,,,#,开始=time.time ()   ,,,#,#,sing_thread =threading.Thread(目标=跳舞,arg游戏=(3)守护进程=True) #设置成守护主线程   ,,,#,sing_thread =, threading.Thread(目标=跳舞,,args=(3))   ,,,#,dance_thread =, threading.Thread(目标=唱,kwargs={“count": 3})   ,,#   ,,,#,sing_thread.start ()   ,,,#,dance_thread.start ()   ,,#   ,,,#,time . sleep (1)   ,,,#,打印(“进程编号:{}主线程结束…用时{}“.format (os.getpid (), (time.time()实体法)))   ,,,for 小姐:拷贝范围(10):#多线程之间执行是无序的,由cpu调度   ,,,,,,,sub_thread =, threading.Thread(目标=任务)   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

python多进程和多线程知识点整理