Python学习笔记——进程和线程

这篇博客详细介绍了Python中的进程和线程,包括os和multiprocessing模块的多进程操作,如fork、Process、Pool,以及线程的创建和同步。还探讨了GIL锁对Python多线程的影响,ThreadLocal的应用,并提到了分布式进程的实现,通过managers模块实现多台机器上的进程通信。

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

http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319272686365ec7ceaeca33428c914edf8f70cca383000


继续学习,接下来是进程和线程。

这篇断断续续学了一个月,已经陷入部分遗忘的状态了,好多是直接从廖老师的教程中贴过来的,还需要再消化消化。


1. 多进程

Python的os模块封装了常见的系统调用,其中就包括fork,可以通过fork创建子进程。该功能只能运行于Unix/Linux/Mac系统,Windows系统无法运行。

fork()函数调用一次,返回两次。父进程的返回子进程的id,子进程的返回0。

import os

print('Process (%s) start...' % os.getpid())
# Only works on Unix/Linux/Mac:
pid = os.fork()
if pid == 0:
    print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
else:
    print('I (%s) just created a child process (%s).' % (os.getpid(), pid))

os.getpid()获取当前进程id。

os.getppid()获取当前进程的父进程id。

multiprocessing模块是跨平台的多进程模块。提供了一个Process类来代表一个进程对象:

from multiprocessing import Process
import os

# 子进程要执行的代码
def run_proc(name):
    print('Run child process %s (%s)...' % (name, os.getpid()))

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

创建子进程需要传入一个执行函数和对应参数构造Process实例,用start()方法启动,用join()方法可以等待子进程结束后再往下进行,可用于进程同步。

如果要启动很多进程,可以使用进程池批量创建子进程:

from multiprocessing import Pool
import os, time, random

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 subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')

Pool对象的join()方法会等待所有子进程执行完毕,调用join()之前必须调用close(),而调用close()之后就不能创建新的进程了。

Pool的默认大小是CPU的核数。

subprocess模块可以用来创建子进程并控制其输入输出:

import subprocess

print('$ nslookup www.python.org')
r = subprocess.call(['nslookup', 'www.python.org'])
print('Exit code:', r)

这和在命令行上直接 执行

nslookup www.python.org

是一样的。

如果需要输入,可以通过communicate()方法:

import subprocess

print('$ nslookup')
p = subprocess.Popen(['nslookup'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate(b'set q=mx\npython.org\nexit\n')
print(output.decode('utf-8'))
print('Exit code:', p.returncode)

相当于运行了nslookup命令后手动输入了:

seq q=mx
python.org
exit

进程间可以通过多种方式来通信,multiprocessing模块提供了Queue、Pipe等方式。

以Queue为例,创建两个子进程,一个写数据,一个读数据:

from multiprocessing import Process, Queue
import os, time, random

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

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

if __name__=='__main__':
    # 父进程创建Queue,并传给各个子进程:
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    # 启动子进程pw,写入:
    pw.start()
    # 启动子进程pr,读取:
    pr.start()
    # 等待pw结束:
    pw.join()
    # pr进程里是死循环,无法等待其结束,只能强行终止:
    pr.terminate()

在windows下,父进程的所有对象都必须通过pickle序列化再传到子进程去。


2. 多线程

一个进程可以由多个线程组成。Python提供对线程的支持,且是真正的Posix Thread,而非模拟出来的线程。

Python提供了两个模块:_thread低级模块和threading高级模块,对_thread模块进行了封装。绝大多数情况使用threading模块就可以了。

启动一个线程就是把一个函数传入并创建Thread实例,然后调用start()开始执行:

import time, threading

# 新线程执行的代码:
def loop():
    print('thread %s is running...' % threading.current_thread().name)
    n = 0
    while n < 5:
        n = n + 1
        print('thread %s >>> %s' % (threading.current_thread().name, n))
        time.sleep(1)
    print('thread %s ended.' % 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 ended.' % 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 ended.
thread MainThread ended.

多进程中,同一个变量,每个进程都有一份自己的拷贝,互不影响;而多线程中,变量是共享的,当多个线程同时操作时,可能会发生无法预知的结果。因此,在关键部分,同一时间只能有一个线程来操作,可以使用锁(Lock)来实现:

balance = 0
lock = threading.Lock()

def run_thread(n):
    for i in range(100000):
        # 先要获取锁:
        lock.acquire()
        try:
            # 放心地改吧:
            change_it(n)
        finally:
            # 改完了一定要释放锁:
            lock.release()

Python的多线程无法利用多核,原因是解释器执行代码时,有一个GIL锁:Global Interpreter Lock,任何Python线程执行前,必须先获得GIL锁,然后,每执行100条字节码,解释器就自动释放GIL锁,让别的线程有机会执行。这个GIL全局锁实际上把所有线程的执行代码都给上了锁,所以,多线程在Python中只能交替执行,即使100个线程跑在100核CPU上,也只能用到1个核。Python虽然不能利用多线程实现多核任务,但可以通过多进程实现多核任务。多个Python进程有各自独立的GIL锁,互不影响。


3. ThreadLocal

如果有个变量,多个函数要用,这样一级级作为参数传递下去,很不方便。

但放到全局变量中又不合适,因为不同线程需要使用不同变量。

一种解决方式是全局有一个dict,使用线程名作为key,但这种方式在获取变量的时候不够优雅。

Python中的ThreadLocal为我们做了这件事:

import threading

# 创建全局ThreadLocal对象:
local_school = threading.local()

def process_student():
    # 获取当前线程关联的student:
    std = local_school.student
    print('Hello, %s (in %s)' % (std, threading.current_thread().name))

def process_thread(name):
    # 绑定ThreadLocal的student:
    local_school.student = name
    process_student()

t1 = threading.Thread(target= process_thread, args=('Alice',), name='Thread-A')
t2 = threading.Thread(target= process_thread, args=('Bob',), name='Thread-B')
t1.start()
t2.start()
t1.join()
t2.join()

输出是:

Hello, Alice (in Thread-A)
Hello, Bob (in Thread-B)

ThreadLocal常用的地方是数据库连接、HTTP请求、用户身份信息等。


4. 分布式进程

Python的multiprocessing模块不但支持多进程,其中的managers子模块还支持把多进程分布到多台机器上。一个服务进程可以作为调度者,将任务分布到其他多个进程中。

例:已有一个通过Queue通信的多进程在一台机器运行,现将发送任务和处理任务的进程分布到两台机器上。原有的Queue可以继续使用,通过managers模块把Queue通过网络暴露出去,就可以让其他机器的进程访问Queue了。

先是服务进程,负责启动Queue,注册到网络上,写入任务:

# task_master.py

import random, time, queue
from multiprocessing.managers import BaseManager

# 发送任务的队列:
task_queue = queue.Queue()
# 接收结果的队列:
result_queue = queue.Queue()

# 从BaseManager继承的QueueManager:
class QueueManager(BaseManager):
    pass

# 把两个Queue都注册到网络上, callable参数关联了Queue对象:
QueueManager.register('get_task_queue', callable=lambda: task_queue)
QueueManager.register('get_result_queue', callable=lambda: result_queue)
# 绑定端口5000, 设置验证码'abc':
manager = QueueManager(address=('', 5000), authkey=b'abc')
# 启动Queue:
manager.start()
# 获得通过网络访问的Queue对象:
task = manager.get_task_queue()
result = manager.get_result_queue()
# 放几个任务进去:
for i in range(10):
    n = random.randint(0, 10000)
    print('Put task %d...' % n)
    task.put(n)
# 从result队列读取结果:
print('Try get results...')
for i in range(10):
    r = result.get(timeout=10)
    print('Result: %s' % r)
# 关闭:
manager.shutdown()
print('master exit.')

这里需要注意的是,在一台机器上,可以直接使用创建的Queue,但是在分布式 环境下,需要通过manager.get_task_queue获得的Queue接口添加,否则就绕过了QueueManager的封装。

然后在另一台机器上(本机也可)启动任务进程:

# task_worker.py

import time, sys, queue
from multiprocessing.managers import BaseManager

# 创建类似的QueueManager:
class QueueManager(BaseManager):
    pass

# 由于这个QueueManager只从网络上获取Queue,所以注册时只提供名字:
QueueManager.register('get_task_queue')
QueueManager.register('get_result_queue')

# 连接到服务器,也就是运行task_master.py的机器:
server_addr = '127.0.0.1'
print('Connect to server %s...' % server_addr)
# 端口和验证码注意保持与task_master.py设置的完全一致:
m = QueueManager(address=(server_addr, 5000), authkey=b'abc')
# 从网络连接:
m.connect()
# 获取Queue的对象:
task = m.get_task_queue()
result = m.get_result_queue()
# 从task队列取任务,并把结果写入result队列:
for i in range(10):
    try:
        n = task.get(timeout=1)
        print('run task %d * %d...' % (n, n))
        r = '%d * %d = %d' % (n, n, n*n)
        time.sleep(1)
        result.put(r)
    except Queue.Empty:
        print('task queue is empty.')
# 处理结束:
print('worker exit.')

最后需要注意的是,Queue的作用使用来传递任务的描述和接收结果,每个任务的描述要尽量小,比如发送一个处理日志的任务,就不要发送很大的日志文件本身,而只需要发送日志文件存放的完整路径即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值