zoukankan      html  css  js  c++  java
  • Python进程和线程

    摘抄自廖雪峰Python教程

    进程和线程

    1.多进程

    Unix/Linux操作系统提供了一个fork()系统调用。普通的函数调用,只会返回一次,但是fork()调用会返回两次。因为操作系统自动把当前进程(父进程)复制了一份(子进程),然后分别在父进程和子进程内返回。

    子进程永远返回0,父进程返回子进程的ID。父进程要记下每个子进程的ID,子进程调用getppid()就可以拿到父进程的ID

    Python的os模块封装了常见的系统调用,其中就包括fork,可以在Python程序中轻松创建子进程:

    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))
    

    运行结果如下:

    Process (876) start...
    I (876) just created a child process (877).
    I am child process (877) and my parent is 876.
    

    multiprocessing

    multiprocessing是Python的多进程模块,跨平台。

    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就是一个子进程
        p.join()
        print('Child process end.')
    

    执行结果如下:

    Parent process 928.
    Child process will start.
    Run child process test (929)...
    Process end.
    

    join()方法可以等待子进程结束后再继续往下运行,通常用于进程间的同步

    Pool

    大量启动子进程,可以使用进程池批量创建子进程:

    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.')
    

    执行结果如下:

    Parent process 669.
    Waiting for all subprocesses done...
    Run task 0 (671)...
    Run task 1 (672)...
    Run task 2 (673)...
    Run task 3 (674)...
    Task 2 runs 0.14 seconds.
    Run task 4 (673)...
    Task 1 runs 0.27 seconds.
    Task 3 runs 0.86 seconds.
    Task 0 runs 1.41 seconds.
    Task 4 runs 1.91 seconds.
    All subprocesses done.
    

    子进程

    subprocess模块可以方便地启动一个子进程,然后控制其输入和输出。

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

    运行结果:

    $ nslookup www.python.org
    Server:        192.168.19.4
    Address:    192.168.19.4#53
    
    Non-authoritative answer:
    www.python.org    canonical name = python.map.fastly.net.
    Name:    python.map.fastly.net
    Address: 199.27.79.223
    
    Exit code: 0
    

    进程间通信

    Process之间肯定需要通信。Python的multiprocessing模块包装了底层的机制,提供了QueuePipes等多种方式交换数据。

    Queue为例,在父进程中创建两个子进程,一个往Queue里写数据,一个从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()
        # join()方法是等待子进程结束
        # pr进程里是死循环,无法等待其结束,只能强行终止:
        pr.terminate()
    

    运行结果如下:

    Process to write: 50563
    Put A to queue...
    Process to read: 50564
    Get A from queue.
    Put B to queue...
    Get B from queue.
    Put C to queue...
    Get C from queue.
    

    2.多线程

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

    进程是由若干线程组成,一个进程至少有一个线程。

    Python的标准库提供了两个模块:_threadthreading_thread是低级模块;threading是高级模块,对_thread进行封装。

    启动一个线程就是把一个函数传入并创建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.  
    

    由于任何进程默认就会启动一个线程,我们把该线程称为主线程,主线程又可以启动新的线程,Python的threading模块有个current_thread()函数,它永远返回当前线程的实例。主线程实例的名字叫MainThread,子线程的名字在创建时指定,我们用LoopThread命名子线程。名字仅仅在打印时用来显示,完全没有其他意义,如果不起名字Python就自动给线程命名为Thread-1Thread-2……

    Lock

    多进程中,同一个变量,各自有一份拷贝存在于每个进程中,互不影响,而多线程中,所有变量都由所有线程共享。所以,任何一个变量都可以被任何一个线程修改。因此,线程之间共享数据最大的危险在于多个线程同时修改一个变量。

    所以我们可以通过创建一个锁来防止修改变量上的冲突,通过threading.Lock()来实现:

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

    当多个线程同时执行lock.acquire()时,只有一个线程能成功获取锁,其他线程需要等待知道获得锁为止(当目前拥有锁的线程执行lock.release()之后)

    多核CPU

    出现一个死循环线程就会100%占用一个CPU核心,正常情况下,启动N个死循环线程就可以跑满N核的CPU

    但是Python不是这样,Python解释器执行代码时,有一个GIL锁:Global Interpreter Lock,任何Python线程执行之前,必须先获得GIL锁,然后每执行100条字节码,解释器就自动释放GIL锁,让别的线程有机会执行。

    所以Python不能高效地利用多线程,但是可以通过多进程实现多核任务。多个Python进程有各自独立的GIL锁,互不影响

    3.ThreadLocal

    多线程在使用全局变量时容易引起冲突,所以使用线程自己的局部变量比较好,但是使用局部变量,在函数调用时的传递参数会很麻烦。

    def process_student(name):
        std = Student(name)
        # std是局部变量,但是每个函数都要用它,因此必须传进去:
        do_task_1(std)
        do_task_2(std)
    
    def do_task_1(std):
        do_subtask_1(std)
        do_subtask_2(std)
    
    def do_task_2(std):
        do_subtask_2(std)
        do_subtask_2(std)
    

    每个函数一层一层调用都这么传参数那还得了?用全局变量?也不行,因为每个线程处理不同的Student对象,不能共享。

    如果用一个全局dict存放所有的Student对象,然后以thread自身作为key获得线程对应的Student对象如何?

    global_dict = {}
    
    def std_thread(name):
        std = Student(name)
        # 把std放到全局变量global_dict中:
        global_dict[threading.current_thread()] = std
        do_task_1()
        do_task_2()
    
    def do_task_1():
        # 不传入std,而是根据当前线程查找:
        std = global_dict[threading.current_thread()]
        ...
    
    def do_task_2():
        # 任何函数都可以查找出当前线程的std变量:
        std = global_dict[threading.current_thread()]
        ...
    

    这种方式理论上是可行的,它最大的优点是消除了std对象在每层函数中的传递问题,但是,每个函数获取std的代码有点丑。

    有更简单的实现方法,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.分布式进程

    在Thread和Process中,应当优选Process,因为Process更稳定,而且,Process可以分布到多台机器上,而Thread最多只能分布到同一台机器的多个CPU上。

    Python的multiprocessing模块不但支持多进程,其中managers子模块还支持把多进程分布到多台机器上。一个服务进程可以作为调度者,将任务分布到其他多个进程中,依靠网络通信。由于managers模块封装很好,不必了解网络通信的细节,就可以很容易地编写分布式多进程程序。

    举个例子:如果我们已经有一个通过Queue通信的多进程程序在同一台机器上运行,现在,由于处理任务的进程任务繁重,希望把发送任务的进程和处理任务的进程分布到两台机器上。怎么用分布式进程实现?

    原有的Queue可以继续使用,但是,通过managers模块把Queue通过网络暴露出去,就可以让其他机器的进程访问Queue了。

    我们先看服务进程,服务进程负责启动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可以直接拿来用,但是,在分布式多进程环境下,添加任务到Queue不可以直接对原始的task_queue进行操作,那样就绕过了QueueManager的封装,必须通过manager.get_task_queue()获得的Queue接口添加。

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

    # 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.')
    

    任务进程要通过网络连接到服务进程,所以要指定服务进程的IP。

    现在,可以试试分布式进程的工作效果了。先启动task_master.py服务进程:

    $ python3 task_master.py 
    Put task 3411...
    Put task 1605...
    Put task 1398...
    Put task 4729...
    Put task 5300...
    Put task 7471...
    Put task 68...
    Put task 4219...
    Put task 339...
    Put task 7866...
    Try get results...
    

    task_master.py进程发送完任务后,开始等待result队列的结果。现在启动task_worker.py进程:

    $ python3 task_worker.py
    Connect to server 127.0.0.1...
    run task 3411 * 3411...
    run task 1605 * 1605...
    run task 1398 * 1398...
    run task 4729 * 4729...
    run task 5300 * 5300...
    run task 7471 * 7471...
    run task 68 * 68...
    run task 4219 * 4219...
    run task 339 * 339...
    run task 7866 * 7866...
    worker exit.
    
    

    task_worker.py进程结束,在task_master.py进程中会继续打印出结果:

    Result: 3411 * 3411 = 11634921
    Result: 1605 * 1605 = 2576025
    Result: 1398 * 1398 = 1954404
    Result: 4729 * 4729 = 22363441
    Result: 5300 * 5300 = 28090000
    Result: 7471 * 7471 = 55815841
    Result: 68 * 68 = 4624
    Result: 4219 * 4219 = 17799961
    Result: 339 * 339 = 114921
    Result: 7866 * 7866 = 61873956
    
    

    这个简单的Master/Worker模型有什么用?其实这就是一个简单但真正的分布式计算,把代码稍加改造,启动多个worker,就可以把任务分布到几台甚至几十台机器上,比如把计算n*n的代码换成发送邮件,就实现了邮件队列的异步发送。

    Queue对象存储在哪?注意到task_worker.py中根本没有创建Queue的代码,所以,Queue对象存储在task_master.py进程中:

    task_master_worker

    Queue之所以能通过网络访问,就是通过QueueManager实现的。由于QueueManager管理的不止一个Queue,所以,要给每个Queue的网络调用接口起个名字,比如get_task_queue

    authkey有什么用?这是为了保证两台机器正常通信,不被其他机器恶意干扰。如果task_worker.pyauthkeytask_master.pyauthkey不一致,肯定连接不上。

  • 相关阅读:
    Codeforces 615D Multipliers (数论)
    第十二届北航程序设计竞赛决赛网络同步赛 J题 两点之间
    ZSTU 4248 KI的目标(dfs)
    POJ2546 Circular Area(计算几何)
    HDU-ACM“菜鸟先飞”冬训系列赛——第7场 H
    Codeforces 761C Dasha and Password(枚举+贪心)
    Codeforces 761D Dasha and Very Difficult Problem(贪心)
    Datastructure
    GDB调试
    GCC操作
  • 原文地址:https://www.cnblogs.com/lambdaCheN/p/7777720.html
Copyright © 2011-2022 走看看