Python学习笔记(十一)多线程

参考资料:https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832360548a6491f20c62d427287739fcfa5d5be1f000

1、进程是由若干线程组成的,一个进程至少有一个线程。多任务可以由多进程完成,也可以由一个进程内的多线程完成。

2、Python的标准库提供了两个模块:thread和threading,thread是低级模块,threading是高级模块,对thread进行了封装。绝大多数情况下,我们只需要使用threading这个高级模块。启动一个线程就是把一个函数传入并创建Thread实例,然后调用start()开始执行。

3、由于任何进程默认就会启动一个线程,即主线程,主线程又可以启动新的线程,Python的threading模块有个current_thread()函数,它永远返回当前线程的实例。主线程实例的名字叫MainThread,子线程的名字在创建时指定。

4、多线程和多进程最大的不同在于,多进程中,同一个变量,各自有一份拷贝存在于每个进程中,互不影响,而多线程中,所有变量都由所有线程共享,所以,任何一个变量都可以被任何一个线程修改,因此,线程之间共享数据最大的危险在于多个线程同时改一个变量,把内容给改乱了。因此,Python提供了threading.lock()方法,在启动线程前创建“锁”,然后在线程改变共享变量前执行lock.acquire()方法,共享变量改变后,执行lock.release()方法,确保多个线程对共享变量修改操作的唯一性(以可预见的顺序进行)。锁的好处就是确保了某段关键代码只能由一个线程从头到尾完整地执行,坏处当然也很多,首先是阻止了多线程并发执行,包含锁的某段代码实际上只能以单线程模式执行,效率就大大地下降了。其次,由于可以存在多个锁,不同的线程持有不同的锁,并试图获取对方持有的锁时,可能会造成死锁,导致多个线程全部挂起,既不能执行,也无法结束,只能靠操作系统强制终止。

5、一般意义上,多线程可以有效利用CPU的多核特性实现多核任务,但由于Python解释器执行代码时,有一个GIL锁:Global Interpreter Lock,任何Python线程执行前,必须先获得GIL锁,然后,每执行100条字节码,解释器就自动释放GIL锁,让别的线程有机会执行。这个GIL全局锁实际上把所有线程的执行代码都给上了锁,所以,多线程在Python中只能交替执行,即使100个线程跑在100核CPU上,也只能用到1个核。因此,Python只能通过多进程实现多核任务。

下面是参考资料中给出的代码示例:

import threading, time

def run_thread():
    print "thread %s running ..." % threading.current_thread().name
    n = 0
    while n < 5:
        n = n + 1
        time.sleep(1)
    print "thread %s end." % threading.current_thread().name
#示例代码一 不涉及共享变量的多线程
def Test():
    t = threading.Thread(target=run_thread, name="myThread")
    t.start()
    t.join()
    print "thread %s end." % threading.current_thread().name
import threading, time

banlance = 0

def change_it(value):
    global banlance
    banlance = banlance + value
    banlance = banlance - value

def run_thread(n):
    for i in range(10000):
        change_it(n)
#示例代码2 未加锁的多线程操作共享变量,每次运行的结果不一致
def Test():
    t1 = threading.Thread(target=run_thread, args=(5,))
    t2 = threading.Thread(target=run_thread, args=(8,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print banlance
import threading, time

banlance = 0
lock = threading.Lock()

def change_it(value):
    global banlance
    banlance = banlance + value
    banlance = banlance - value

def run_thread(n):
    for i in range(10000):
        lock.acquire()
        try:
            change_it(n)
        finally:
            lock.release()
#示例代码3 加锁方式多线程操作共享变量,每次运行后banlance的值均为0
def Test():
    t1 = threading.Thread(target=run_thread, args=(5,))
    t2 = threading.Thread(target=run_thread, args=(8,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print banlance
今天就学习到这里,下一节从多线程的ThreadLocal对象学起。

猜你喜欢

转载自blog.csdn.net/alvin_2005/article/details/80414263