【Python复习巩固 Day6】多进程和多线程

本文探讨了多任务在操作系统中的实现,重点介绍了Python中的多进程和多线程。通过创建Process实例和使用进程池展示了多进程操作,强调了使用Queue实现进程间通信。同时,解释了Python的GIL锁限制了多线程的并行计算能力,但可通过多进程实现多核利用。最后,提到了多线程在Python中的应用和限制,并鼓励读者交流指正。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现代很多操作系统都是支持多任务的操作系统,简单的说,就是操作系统可以同时运行多个任务,你可以一边听音乐,一边打游戏,这就是多任务

对操作系统来说,一个任务就是一个进程(Process),有些进程不止做一件事情,在进程内部,要同时干多件事情,就需要同时运行多个子任务,我们把进程内的这些子任务称为线程(Thread)

线程是最小的执行单元,而进程由至少一个线程组成。如何调度进程和线程,完全由操作系统决定,程序自己不能决定什么时候执行,执行多长时间

多任务的实现方式

  • 多进程模式
  • 多线程模式
  • 多进程+多线程模式

Python既支持多进程,又支持多线程

多进程

"""
多进程
"""
from multiprocessing import Process
import os


def run_process(name):
    print('Run child process %s (%s)' % (name, os.getpid()))


if __name__ == '__main__':
    print('Parent process %s' % os.getpid())
    p = Process(target=run_process, args=('process1', ))
    p.start()
    p.join()
    print('Child process end')

Parent process 13728
Run child process process1 (9944)
Child process end

Process finished with exit code 0

创建子进程时,只需要传入一个执行函数和函数的参数,就可以创建一个Process实例了,可以用start()方法来启动,join()方法可以等待子进程结束后再继续往下运行,通常用于进程间的同步

如果我们要启动大量子进程,可以使用进程池的方式批量创建子进程

"""
进程池
"""
import os
import random
import time
from multiprocessing import Pool


def long_time_task(name):
    print('Run task %s (%s)' % (name, os.getpid()))
    start = time.time()
    time.sleep(random.random() * 3)
    end = time.time()
    print('Task %s runs %0.2f seconds' % (name, (end - start)))


if __name__ == '__main__':
    print('Parent process %s' % os.getpid())
    p = Pool(4)
    for i in range(5):
        p.apply_async(long_time_task, args=(i,))
    print('Waiting for all subprocess done...')
    p.close()
    p.join()
    print('All subprocess done')

Parent process 11100
Waiting for all subprocess done...
Run task 0 (11508)
Run task 1 (11612)
Run task 2 (14016)
Run task 3 (1016)
Task 0 runs 0.03 seconds
Run task 4 (11508)
Task 3 runs 0.06 seconds
Task 1 runs 0.36 seconds
Task 2 runs 0.71 seconds
Task 4 runs 2.27 seconds
All subprocess done

Process finished with exit code 0

我们还可以用subprocess模块来启动一个子进程,然后控制其输入和输出

"""
子进程
"""
import subprocess

print('ping')
p = subprocess.Popen(['ping', 'www.python.org'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate()
print(output.decode('gbk'))
print('exit code: ', p.returncode)

ping

正在 Ping dualstack.python.map.fastly.net [151.101.108.223] 具有 32 字节的数据:
来自 151.101.108.223 的回复: 字节=32 时间=97ms TTL=52
来自 151.101.108.223 的回复: 字节=32 时间=111ms TTL=52
来自 151.101.108.223 的回复: 字节=32 时间=139ms TTL=52
来自 151.101.108.223 的回复: 字节=32 时间=137ms TTL=52

151.101.108.223 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 97ms,最长 = 139ms,平均 = 121ms

exit code:  0

Process finished with exit code 0

eg:使用Queue实现进程之间的通信
在父进程中创建两个子进程,一个往Queue里写数据,一个从Queue里读数据

"""
进程之间的通信
"""
import os
import random
import time
from multiprocessing import Process, Queue


def write_data(queue1):
    """
    写数据进程执行的代码
    :param queue1:
    :return:
    """
    print('Process to write: %s' % os.getpid())
    for value in ['A', 'B', 'C']:
        print('Put %s to queue' % value)
        queue1.put(value)
        time.sleep(random.random())


def read_data(queue1):
    """
    读数据进程执行的代码
    :param queue1:
    :return:
    """
    print('Process to read: %s' % os.getpid())
    while True:
        value = queue1.get(True)
        print('Get %s from queue' % value)


if __name__ == '__main__':
    q = Queue()
    pw = Process(target=write_data, args=(q,))
    pr = Process(target=read_data, args=(q,))
    pw.start()
    pr.start()
    pw.join()
    pr.terminate()

Process to read: 14992
Process to write: 9172
Put A to queue
Get A from queue
Put B to queue
Get B from queue
Put C to queue
Get C from queue

Process finished with exit code 0

多线程

多任务可以由多进程完成,也可以由一个进程内的多线程完成

"""
多线程
"""
import threading
import time


def loop():
    print('thread %s is running' % threading.current_thread().name)
    n = 0
    while n < 5:
        n += 1
        print('thread %s >>> %s' % (threading.current_thread().name, n))
        time.sleep(1)
    print('thread %s end' % threading.current_thread().name)


print('thread %s is running' % threading.current_thread().name)
t = threading.Thread(target=loop, name='LoopThread')
t.start()
t.join()
print('thread %s end' % threading.current_thread().name)

thread MainThread is running
thread LoopThread is running
thread LoopThread >>> 1
thread LoopThread >>> 2
thread LoopThread >>> 3
thread LoopThread >>> 4
thread LoopThread >>> 5
thread LoopThread end
thread MainThread end

Process finished with exit code 0

注意:由于Python解释器在执行代码时,有一个GIL锁,因此,可以在Python使用多线程,但是不要指望能有效利用多核,不过,也不用太担心,Python虽然不能利用多线程实现多核任务,但是可以通过多进程实现多核任务。多个Python进程有各自独立的GIL锁,互不影响


在这里插入图片描述

蒟蒻写博客不易,加之本人水平有限,写作仓促,错误和不足之处在所难免,谨请读者和各位大佬们批评指正。
如需转载,请署名作者并附上原文链接,蒟蒻非常感激
名称:虐猫人薛定谔
博客地址:https://blog.csdn.net/Deep___Learning

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值