欢迎访问宙启技术站
智能推送

Python中的多线程与多进程处理函数解析

发布时间:2023-06-23 20:10:34

在Python中,多线程和多进程是实现并行计算的两种方式。它们可以用来提高程序的运行效率和响应能力。本文将对Python多线程和多进程的处理函数进行详细解析,包括线程和进程的概念,线程和进程的创建方式,以及常用的线程和进程处理函数。

一、线程和进程的概念

线程是指在一个程序中同时执行的多个任务,每个任务都可以被称为一个线程。线程共享同一个进程的内存空间,因此它们可以共享数据和信息。在相同的内存地址空间中,线程间切换的代价比较小,速度比较快。

进程是指正在运行的程序的实体,它是操作系统分配资源的基本单位。每个进程都有自己的独立地址空间和内存空间,包含程序代码、数据、堆栈等。进程间通信的代价比较大,速度比较慢。

二、线程和进程的创建方式

1. 线程的创建方式

Python中的线程创建方式有两种:

(1)继承Thread类,重写run()方法,并用start()方法启动线程。

import threading

class my_Thread(threading.Thread):
    def __init__(self,id):
        threading.Thread.__init__(self)
        self.id=id
    def run(self):
        print("当前线程ID为:",self.id)
 
#启动10个线程
for i in range(10):
    t=my_Thread(i)
    t.start()

(2)使用函数式编程,使用threading模块的Thread函数创建线程。

import threading

def my_Thread(id):
    print("当前线程ID为:",id)

#启动10个线程
for i in range(10):
    t=threading.Thread(target=my_Thread,args=(i,))
    t.start()

2. 进程的创建方式

Python中的进程创建方式有两种:

(1)使用multiprocessing模块的Process函数创建进程。

import multiprocessing

def my_Process(id):
    print("当前进程ID为:",id)

#启动10个进程
for i in range(10):
    p=multiprocessing.Process(target=my_Process,args=(i,))
    p.start()

(2)继承Process类,重写run()方法,并用start()方法启动进程。

import multiprocessing

class my_Process(multiprocessing.Process):
    def __init__(self,id):
        multiprocessing.Process.__init__(self)
        self.id=id
    def run(self):
        print("当前进程ID为:",self.id)

#启动10个进程
for i in range(10):
    p=my_Process(i)
    p.start()

三、常用的线程和进程处理函数

1. 线程处理函数

(1)current_thread()函数

current_thread()函数返回当前线程的实例。

import threading

def my_Thread():
    print("当前线程ID为:",threading.current_thread().ident)

#启动5个线程
for i in range(5):
    t=threading.Thread(target=my_Thread)
    t.start()

(2)Thread.join()函数

join()函数会阻塞主线程,直到该线程执行完毕。

import threading

def my_Thread():
    print("当前线程ID为:",threading.current_thread().ident)

#启动5个线程
threads=[]
for i in range(5):
    t=threading.Thread(target=my_Thread)
    threads.append(t)
    t.start()

#主线程等待所有子线程执行完毕
for t in threads:
    t.join()

(3)Thread.is_alive()函数

is_alive()函数用来检查线程是否存活。

import threading

def my_Thread():
    print("当前线程ID为:",threading.current_thread().ident)

t=threading.Thread(target=my_Thread)
t.start()

if not t.is_alive():
    print("线程已经死亡")

2. 进程处理函数

(1)current_process()函数

current_process()函数返回当前进程的实例。

import multiprocessing

def my_Process():
    print("当前进程ID为:",multiprocessing.current_process().ident)

#启动5个进程
for i in range(5):
    p=multiprocessing.Process(target=my_Process)
    p.start()

(2)Process.join()函数

join()函数会阻塞主进程,直到该进程执行完毕。

import multiprocessing

def my_Process():
    print("当前进程ID为:",multiprocessing.current_process().ident)

#启动5个进程
processes=[]
for i in range(5):
    p=multiprocessing.Process(target=my_Process)
    processes.append(p)
    p.start()

#主进程等待所有子进程执行完毕
for p in processes:
    p.join()

(3)Process.is_alive()函数

is_alive()函数用来检查进程是否存活。

import multiprocessing

def my_Process():
    print("当前进程ID为:",multiprocessing.current_process().ident)

p=multiprocessing.Process(target=my_Process)
p.start()

if not p.is_alive():
    print("进程已经死亡")

四、总结

上述是Python中多线程和多进程的概念、创建方式和常用处理函数的解析。线程和进程的区别和联系在本文中都有提到,线程和进程的选择应该根据实际需求来定。多线程可以提高单个程序的处理效率,但不能提高计算机的性能,而多进程则可以充分利用计算机的多核处理器,提高整个系统的运行效率。