3

Python进程间通信之Manager、全局解释器锁GIL、定时器线程Timer | CHEGVA

 1 year ago
source link: https://chegva.com/5629.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python高级(18)—进程间通信之Manager、全局解释器锁GIL、定时器线程Timer

◎知识点

  1. 进程间通信之Manager

  2. 全局解释器锁GIL

  3. 定时器线程Timer

◎脚本练习

▽ 进程间通信之Manager

"""
    如果想要实现进程之间的通信,Manager也是常见的实现方式之一。
    与共享内存相比,Manager更加灵活,因为它可以支持多种对象类型。此外,Manager还可以通过网络
被不同计算机上的进程所共享。但是,Manager的速度比共享内存慢。
"""

from multiprocessing import Process, Manager


def f(d, l):
    d[1] = 18
    d['2'] = 56
    l.reverse()


if __name__ == '__main__':

    manager = Manager()

    d = manager.dict()

    l = manager.list(range(5))

    p = Process(target=f, args=(d, l))
    p.start()
    p.join()

    print(d)    # {1: 18, '2': 56}
    print(l)    # [4, 3, 2, 1, 0]
Python

▽ 全局解释器锁GIL

"""
def do_sth():
    while True:
        pass

do_sth()

    单进程或单线程占满了八核CPU中的其中一核。
"""

"""
from multiprocessing import Process

def do_sth():
    while True:
        pass

if __name__ == '__main__':

    Process(target=do_sth).start()
    Process(target=do_sth).start()

    do_sth()

    三个进程占满了八核CPU中的其中三核。因此,多进程可以实现并行(同时处理多个任务)从而发挥
多核CPU的最大功效。
"""

"""
from threading import Thread

def do_sth():
    while True:
        pass

Thread(target=do_sth).start()
Thread(target=do_sth).start()

do_sth()

    三个线程并没有占满八核CPU中的其中三核,而只占满了其中一核,因此,多线程并不能实现并行
(同时处理多个任务)而只能实现并发(交替处理多个任务)。
"""

"""
    我们编写的python代码是通过python解释器来执行的。通常使用的python解释器是官方提供的CPython。
CPython中有一个GIL(Global Interpreter Lock,全局解释器锁),其作用相当于Lock,任何线程
在执行前必须先获得GIL,一个线程在获得GIL后其它线程就不能执行,直到线程释放GIL。因此,GIL保证了
同一时刻只有一个线程可以执行,从而导致python中的多线程不能实现并行
"""
Python

▽ 定时器线程Timer

"""
    如果想要在指定的时间片段之后再启动子线程,可以使用标准库模块threading提供的类对象Timer,
用于表示定时器线程。Timer是Thread的子类,也是通过调用方法start()来启动线程。
"""

"""
from threading import Timer

def do_sth():
    print('Hello Timer!')

timer = Timer(2, do_sth)
timer.start()
"""

"""
    定时器只执行一次。如果需要每隔一段时间执行一次,则需要在子线程调用的函数的内部再次创建与启动
定时器线程。
"""

from threading import Timer
import time

def do_sth():
    print('Hello Timer!')
    global timer
    timer = Timer(3, do_sth)
    timer.start()

timer = Timer(2, do_sth)
timer.start()

time.sleep(10)
timer.cancel()
Python

◎脚本地址:https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_senior/manager_gil_timer.py

安志合个人博客,版权所有 丨 如未注明,均为原创 丨 转载请注明转自:https://chegva.com/5629.html | ☆★★每天进步一点点,加油!★★☆ | 

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK