您的位置:首页技术文章
文章详情页

Python之多进程与多线程的使用

【字号: 日期:2022-06-27 11:24:20浏览:36作者:猪猪
进程与线程

想象在学校的一个机房,有固定数量的电脑,老师安排了一个爬虫任务让大家一起完成,每个学生使用一台电脑爬取部分数据,将数据放到一个公共数据库。共同资源就像公共数据库,进程就像每一个学生,每多一个学生,就多一个进程来完成这个任务,机房里的电脑数量就像CPU,所以进程数量是CPU决定的,线程就像学生用一台电脑开多个爬虫,爬虫数量由每台电脑的运行内存决定。一个CPU可以有多个进程,一个进程有一个或多个线程。

多进程

1、导包

from multiprocessing import Process

2、写两个任务也就是两个函数

3、创建一个进程进程名字 = Process(target=函数名字,函数参数传字典或元组,是否守护进程)

4、启动进程进程名字.start()

5、是否开启进程守护,一般主进程会等待子进程执行完毕后再关闭程序。当我们想程序主进程跑完,直接销毁掉未完成的子进程,关闭程序的话,加上一句代码 :1.创建进程的时候传参数daemon=True2.进程名字.daemon=True

6、进程编号导包os获取当前进程编号

os.getpid()

获取当前父进程的编号

os.getppid()

代码示例(未开启进程守护)

from multiprocessing import Processimport timeimport os# 一个写作业函数def homeWork(name, count): for i in range(count): # 打印当前进程编号os.getpid() print('当前进程编号:', os.getpid()) # 打印当前父进程编号os.getppid() print('当前父进程编号:', os.getppid()) print(name, '正在写作业...') time.sleep(0.2)# 一个打游戏函数def game(name, count): for i in range(count): # 打印当前进程编号os.getpid() print('当前进程编号:', os.getpid()) # 打印当前父进程编号os.getppid() print('当前父进程编号:', os.getppid()) print(name, '正在打游戏...') time.sleep(0.2)if __name__ == ’__main__’: # 打印当前进程编号os.getpid() print('当前进程编号:', os.getpid()) # 进程1写作业 元组传参 p1 = Process(target=homeWork, args=('进程1', 10)) # 进程2打游戏 字典传参 p2 = Process(target=game, kwargs={'name': '进程2', 'count': 10}) # 启动进程 p1.start() p2.start() time.sleep(1) print('主进程结束---------------------------------------------')

未开启线程守护的运行结果:

# 可以看到主进程结束的,其子进程还在继续当前进程编号: 14972当前进程编号: 5732当前父进程编号: 14972进程1 正在写作业...当前进程编号: 14752当前父进程编号: 14972进程2 正在打游戏...当前进程编号: 5732当前父进程编号: 14972进程1 正在写作业...当前进程编号: 14752当前父进程编号: 14972进程2 正在打游戏...当前进程编号: 5732当前父进程编号: 14972进程1 正在写作业...当前进程编号: 14752当前父进程编号: 14972进程2 正在打游戏...当前进程编号: 5732当前父进程编号: 14972进程1 正在写作业...当前进程编号: 14752当前父进程编号: 14972进程2 正在打游戏...主进程结束---------------------------------------------当前进程编号: 5732当前父进程编号: 14972进程1 正在写作业...当前进程编号: 14752当前父进程编号: 14972进程2 正在打游戏...当前进程编号: 5732当前父进程编号: 14972进程1 正在写作业...当前进程编号: 14752当前父进程编号: 14972进程2 正在打游戏...当前进程编号: 5732当前父进程编号: 14972进程1 正在写作业...当前进程编号: 14752当前父进程编号: 14972进程2 正在打游戏...当前进程编号: 5732当前父进程编号: 14972进程1 正在写作业...当前进程编号: 14752当前父进程编号: 14972进程2 正在打游戏...当前进程编号: 5732当前父进程编号: 14972进程1 正在写作业...当前进程编号: 14752当前父进程编号: 14972进程2 正在打游戏...当前进程编号: 5732当前父进程编号: 14972进程1 正在写作业...当前进程编号: 14752当前父进程编号: 14972进程2 正在打游戏...

Process finished with exit code 0

代码示例(开启进程守护)

from multiprocessing import Processimport timeimport os# 一个写作业函数def homeWork(name, count): for i in range(count): # 打印当前进程编号os.getpid() print('当前进程编号:', os.getpid()) # 打印当前父进程编号os.getppid() print('当前父进程编号:', os.getppid()) print(name, '正在写作业...') time.sleep(0.2)# 一个打游戏函数def game(name, count): for i in range(count): # 打印当前进程编号os.getpid() print('当前进程编号:', os.getpid()) # 打印当前父进程编号os.getppid() print('当前父进程编号:', os.getppid()) print(name, '正在打游戏...') time.sleep(0.2)if __name__ == ’__main__’: # 打印当前进程编号os.getpid() print('当前进程编号:', os.getpid()) # 进程1写作业 元组传参 第一种方法启动进程守护 p1 = Process(target=homeWork, args=('进程1', 10), daemon=True) # 进程2打游戏 字典传参 p2 = Process(target=game, kwargs={'name': '进程2', 'count': 10}) # 第二种 p2.daemon = True # 启动进程 p1.start() p2.start() time.sleep(1) print('主进程---------------------------------------------')

开启进程守护的运行结果

当前进程编号: 372当前进程编号: 10116当前进程编号: 9860当前父进程编号: 372进程1 正在写作业...当前父进程编号: 372进程2 正在打游戏...当前进程编号: 9860当前进程编号: 10116当前父进程编号: 372进程2 正在打游戏...当前父进程编号: 372进程1 正在写作业...当前进程编号: 9860当前进程编号: 10116当前父进程编号: 372进程1 正在写作业...当前父进程编号: 372进程2 正在打游戏...当前进程编号: 9860当前进程编号: 10116当前父进程编号: 372进程1 正在写作业...当前父进程编号: 372进程2 正在打游戏...主进程结束---------------------------------------------

Process finished with exit code 0

多线程

1、导包

import threading

2、写两个任务也就是两个函数

3、创建一个线程线程名字 = threading.Thread(target=函数名字,函数参数传字典或元组,是否守护进程)

4、启动线程线程名字.start()

5、是否开启线程守护,一般当前程序会等待子线程执行完毕后再关闭程序。当我们想程序跑完,销毁掉未完成的子线程,直接关闭程序的话,加上一句代码 :1.创建线程的时候传参数daemon=True2.线程名字.daemon=True

6、线程编号获取当前线程编号

threading.current_thread()

代码示例(未开启进程守护)

import threadingimport time# 一个写作业函数def homeWork(name, count): for i in range(count): # 打印当前线程 print(threading.current_thread()) print(name, '正在写作业...') time.sleep(0.2)# 一个打游戏函数def game(name, count): for i in range(count): # 打印当前线程 print(threading.current_thread()) print(name, '正在打游戏...') time.sleep(0.2)if __name__ == ’__main__’: # 线程1写作业 元组传参 t1 = threading.Thread(target=homeWork, args=('进程1', 10)) # 线程2打游戏 字典传参 t2 = threading.Thread(target=game, kwargs={'name': '进程2', 'count': 10}) # 启动进程 t1.start() t2.start() time.sleep(1) print('主进程结束###################################################################################')

未开启线程守护的运行结果

# 可以看到主进程结束的,其线程还在继续<Thread(Thread-1, started 3364)>进程1 正在写作业...<Thread(Thread-2, started 9100)>进程2 正在打游戏...<Thread(Thread-2, started 9100)>进程2 正在打游戏...<Thread(Thread-1, started 3364)>进程1 正在写作业...<Thread(Thread-1, started 3364)>进程1 正在写作业...<Thread(Thread-2, started 9100)>进程2 正在打游戏...<Thread(Thread-2, started 9100)>进程2 正在打游戏...<Thread(Thread-1, started 3364)>进程1 正在写作业...<Thread(Thread-1, started 3364)>进程1 正在写作业...<Thread(Thread-2, started 9100)>进程2 正在打游戏...主进程结束###################################################################################<Thread(Thread-2, started 9100)>进程2 正在打游戏...<Thread(Thread-1, started 3364)>进程1 正在写作业...<Thread(Thread-1, started 3364)><Thread(Thread-2, started 9100)>进程2 正在打游戏...进程1 正在写作业...<Thread(Thread-1, started 3364)>进程1 正在写作业...<Thread(Thread-2, started 9100)>进程2 正在打游戏...<Thread(Thread-2, started 9100)><Thread(Thread-1, started 3364)>进程1 进程2正在写作业... 正在打游戏...<Thread(Thread-2, started 9100)><Thread(Thread-1, started 3364)>

进程2 进程1 正在打游戏...正在写作业...

Process finished with exit code 0

代码示例(开启线程守护)

import threadingimport time# 一个写作业函数def homeWork(name, count): for i in range(count): # 打印当前线程 print(threading.current_thread()) print(name, '正在写作业...') time.sleep(0.2)# 一个打游戏函数def game(name, count): for i in range(count): # 打印当前线程 print(threading.current_thread()) print(name, '正在打游戏...') time.sleep(0.2)if __name__ == ’__main__’: # 线程1写作业 元组传参 t1 = threading.Thread(target=homeWork, args=('进程1', 10), daemon=True) # 线程2打游戏 字典传参 t2 = threading.Thread(target=game, kwargs={'name': '进程2', 'count': 10}) t2.daemon = True # 启动进程 t1.start() t2.start() time.sleep(1) print('主进程结束###################################################################################')

开启线程守护的运行结果

<Thread(Thread-1, started daemon 15480)>进程1 正在写作业...<Thread(Thread-2, started daemon 13700)>进程2 正在打游戏...<Thread(Thread-2, started daemon 13700)>进程2 正在打游戏...<Thread(Thread-1, started daemon 15480)>进程1 正在写作业...<Thread(Thread-1, started daemon 15480)><Thread(Thread-2, started daemon 13700)>进程1 进程2 正在写作业...正在打游戏...

<Thread(Thread-2, started daemon 13700)><Thread(Thread-1, started daemon 15480)>

进程1进程2 正在写作业... 正在打游戏...

<Thread(Thread-1, started daemon 15480)>进程1 正在写作业...<Thread(Thread-2, started daemon 13700)>进程2 正在打游戏...主进程结束###################################################################################

Process finished with exit code 0

到此这篇关于Python之多进程与多线程的使用的文章就介绍到这了,更多相关Python 多进程与多线程内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Python 编程
相关文章: