26 python 初学(线程同步锁死锁和递归锁)

Posted mlllily

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了26 python 初学(线程同步锁死锁和递归锁)相关的知识,希望对你有一定的参考价值。

参考博客: www.cnblogs.com/yuanchenqi/articles/5733873.html

并发:一段时间内做一些事情

并行:同时做多件事情

线程是操作系统能够进行运算调度的基本单位,一个线程就是一个指令集

IO 密集型任务或函数  计算密集型任务函数

t1 = threading.Thread( target=foo, args=( , ))

t1.start()

# _author: lily
# _date: 2019/1/29

import threading
import time


class MyThread(threading.Thread):
    def __init__(self, num):
        threading.Thread.__init__(self)
        self.num = num

    def run(self):   # 定义每个线程要运行的函数
        print(\'running on number %s\' % self.num)
        time.sleep(3)


if __name__ == \'__main__\':
    t1 = MyThread(1)
    t2 = MyThread(2)
    t1.start()
    t2.start()
View Code
# _author: lily
# _date: 2019/1/28

import threading
import time

def music(func):
    for i in range(2):
        print(\'listening to music %s.%s\' % (func, time.ctime()))
        time.sleep(1)
        print(\'end listening %s\' % time.ctime())

def move(func):
    for i in range(2):
        print(\'watching at the %s.%s\' % (func, time.ctime()))
        time.sleep(5)
        print(\'end watching %s\' % time.ctime())

threads = []
t1 = threading.Thread(target=music, args=(\'七里香\', ))
threads.append(t1)
t2 = threading.Thread(target=move, args=(\'阿甘正传\', ))
threads.append(t2)
if __name__ == \'__main__\':
    for t in threads:
        t.start()
    print(\'all over %s\' % time.ctime())
View Code

 

GIL: 全局解释器锁。 对于一个进程,在同一时刻,python解释器中只允许一个线程运行。

 

结论:在 python里,如果是 io 密集型,可以用多线程

 

                                             计算密集型,改 C

 

守护线程: t.setDaemon(True)  当主线程结束之后就认为程序执行完毕,不会等待 t 线程执行完毕。

 

得到当前线程  print(threading.current_thread())

 

得到当前活着的线程 print(threading.active_count())

 

同步锁:

原因:1. 线程共享同一资源,且进行 IO 阻塞时,对资源的操作容易被覆盖

  1. 使用 join 就会造成船串行,失去了多线程的意义

使用:r = threading.Lock()

同步锁与GIL关系:

没有GIL ,使用同步锁,可以达到一样得效果。

 

# _author: lily
# _date: 2019/1/29

import time
import threading

num = 100

def add():
    global num
    # num -= 1

    r.acquire()
    temp = num
    # time.sleep(0.0000001)
    print(\'ok\')
    num = temp - 1
    r.release()

thread_list = []

r = threading.Lock()
for i in range(100):
    t = threading.Thread(target=add)
    t.start()
    thread_list.append(t)

for thd in thread_list:
    thd.join()

print(\'final num: \', num)
View Code

 

 

线程死锁和递归锁:

lock = threading.Lock()

lock = threading.RLock()

 

# _author: lily
# _date: 2019/1/29
import threading
import time

class MyThread(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name

    def run(self):
        self.do_a()
        self.do_b()

    def do_a(self):
        # lock_a.acquire()
        my_lock.acquire()
        print(\'do_a:  thread %s get lock A\' % self.name)
        time.sleep(3)
        my_lock.acquire()
        print(\'do_a:  thread %s get lock B\' % self.name)
        # lock_b.release()
        # lock_a.release()
        my_lock.release()
        my_lock.release()

    def do_b(self):
        # lock_b.acquire()
        my_lock.acquire()
        print(\'do_b:  thread %s get lock B\' % self.name)
        time.sleep(2)
        # lock_a.acquire()
        my_lock.acquire()
        print(\'do_b:  thread %s get lock A\' % self.name)
        # lock_a.release()
        # lock_b.release()
        my_lock.release()
        my_lock.release()




# lock_a = threading.Lock()
# lock_b = threading.Lock()
my_lock = threading.RLock()
thread_list = []

for i in range(5):
    t = MyThread(i)
    thread_list.append(t)
    t.start()

for t in thread_list:
    t.join()
View Code

 

以上是关于26 python 初学(线程同步锁死锁和递归锁)的主要内容,如果未能解决你的问题,请参考以下文章

Python学习第47天(递归锁同步对象信号量)

python学习第37天GIL锁死锁现象与递归锁信号量Event时间线程queue

23Java并发性和多线程-重入锁死

Python入门学习-DAY36-GIL全局解释器锁死锁现象与递归锁信号量Event事件线程queue

GIL全局解释器锁死锁递归锁信号量Event事件线程Queue

并发&并行 同步&异步 GIL 任务 同步锁 死锁 递归锁