#!/usr/bin/env python
-- coding:utf-8 --
Thread is not it will generate data insecurity
# 共享内存
a = 0
def add_f():
overall to
for i in range(200000):
a += 1
def sub_f():
overall to
for i in range(200000):
a -= 1
from threading import Thread
t1 = Thread(target=add_f)
t1.start()
t2 = Thread(target=sub_f)
t2.start()
t1.join()
t2.join()
print(a)
a = 0
def func():
overall to
a -= 1
import dis
dis.dis(func)
Even the thread even with the GIL data will also appear the problem of insecurity
# 1.操作的是全局变量
# 2.做一下操作
# += -= *= /+ 先计算再赋值才容易出现数据不安全的问题
# 包括 lst[0] += 1 dic['key']-=1
a = 0
def func():
overall to
a += 1
import dis
dis.dis(func)
a = 0
def add_f(lock):
global a
for i in range(200000):
with lock:
a += 1
def sub_f(lock):
global a
for i in range(200000):
with lock:
a -= 1
from threading import Thread,Lock
lock = Lock()
t1 = Thread(target=add_f,args=(lock,))
t1.start()
t2 = Thread(target=sub_f,args=(lock,))
t2.start()
t1.join()
t2.join()
print(a)
Locking can affect the performance of programs, but to ensure the security of data
Mutex is a lock: In the same thread, you can not acquire many times in a row
from threading import Lock
lock = Lock()
lock.acquire()
print(’*’*20)
lock.release()
lock.acquire()
print(’-’*20)
lock.release()
Single mode
#!/usr/bin/env python
-- coding:utf-8 --
import time
from threading import Lock
class A:
__instance = None
lock = Lock()
def new(cls, *args, **kwargs):
with cls.lock:
if not cls.__instance:
time.sleep(0.1)
cls.__instance = super().new(cls)
return cls.__instance
def init(self,name,age):
self.name = name
self.age = age
def func():
a = A(‘alex’, 84)
print(a)
from threading import Thread
for i in range(10):
t = Thread(target=func)
t.start()
#!/usr/bin/env python
-- coding:utf-8 --
Time Import
from the Thread Threading Import, Lock
noodle_lock = Lock ()
fork_lock = Lock ()
DEF eat1 (name, noodle_lock, fork_lock):
noodle_lock.acquire ()
Print ( '% S grab surface of' name%)
fork_lock.acquire ( )
Print ( '% S grab a fork'% name)
Print ( '% S a bite plane'% name)
the time.sleep (0.1)
fork_lock.release ()
Print ( '% S down the fork'% name)
noodle_lock.release ()
Print ( '% S placed below the'% name)
eat2 DEF (name, noodle_lock, fork_lock):
fork_lock.acquire ()
Print ( '% S grab a fork' name%)
noodle_lock.acquire ()
Print ( '% S grab surface of' name%)
Print ( '% s eating a surface '% name)
the time.sleep (0.1)
noodle_lock.release ()
Print ('% s placed below the '% name)
fork_lock.release ()
Print ('% s down the fork '% name)
lst = [‘alex’,‘wusir’,‘taibai’,‘yuan’]
Thread(target=eat1,args=(lst[0],noodle_lock,fork_lock)).start()
Thread(target=eat2,args=(lst[1],noodle_lock,fork_lock)).start()
Thread(target=eat1,args=(lst[2],noodle_lock,fork_lock)).start()
Thread(target=eat2,args=(lst[3],noodle_lock,fork_lock)).start()
lock
# 互斥锁
# 在一个线程中连续多次acquire会死锁
# 递归锁
# 在一个线程中连续多次acquire不会死锁
# 死锁现象
# 死锁现象是怎么发生的?
# 1.有多把锁,一把以上
# 2.多把锁交替使用
# 怎么解决
# 递归锁 —— 将多把互斥锁变成了一把递归锁
# 快速解决问题
# 效率差
# ***递归锁也会发生死锁现象,多把锁交替使用的时候
# 优化代码逻辑
# 可以使用互斥锁 解决问题
# 效率相对好
# 解决问题的效率相对低
#!/usr/bin/env python
-- coding:utf-8 --
from threading import RLock
rlock = RLock()
rlock.acquire()
print(’*’*20)
rlock.acquire()
print(’-’*20)
rlock.acquire()
print(’*’*20)
In the same thread, it can not be locked many times in a row acuqire
Recursive lock:
# 好 :在同一个进程中多次acquire也不会发生阻塞
# 不好 :占用了更多资源
import time
from threading import RLock,Thread
noodle_lock = RLock()
fork_lock = RLock()
= = fork_lock RLOCK noodle_lock ()
Print (noodle_lock, fork_lock)
DEF eat1 (name, noodle_lock, fork_lock):
noodle_lock.acquire ()
Print ( '% S grab surface of' name%)
fork_lock.acquire ()
Print ( '% s grab a fork 'name%)
Print ('% s eating a surface 'name%)
the time.sleep (0.1)
fork_lock.release ()
Print ('% s down the fork 'name%)
noodle_lock.release ()
print ( '% s placed below the'% name)
eat2 DEF (name, noodle_lock, fork_lock):
fork_lock.acquire ()
Print ( '% S grab a fork' name%)
noodle_lock.acquire ()
Print ( '% S grab surface of' name%)
Print ( '% s eating a surface '% name)
the time.sleep (0.1)
noodle_lock.release ()
Print ('% s placed below the '% name)
fork_lock.release ()
Print ('% s down the fork '% name)
lst = [‘alex’,‘wusir’,‘taibai’,‘yuan’]
Thread(target=eat1,args=(lst[0],noodle_lock,fork_lock)).start()
Thread(target=eat2,args=(lst[1],noodle_lock,fork_lock)).start()
Thread(target=eat1,args=(lst[2],noodle_lock,fork_lock)).start()
Thread(target=eat2,args=(lst[3],noodle_lock,fork_lock)).start()
#!/usr/bin/env python
-- coding:utf-8 --
Time Import
from Import Threading Lock, the Thread
Lock Lock = ()
DEF eat1 (name, noodle_lock, fork_lock):
lock.acquire ()
Print ( '% S grab surface of' name%)
Print ( '% S grab the fork 'name%)
Print ('% S a bite plane 'name%)
the time.sleep (0.1)
Print ('% S down the fork 'name%)
Print ('% S placed below the 'name%)
lock.release ()
eat2 DEF (name, noodle_lock, fork_lock):
lock.acquire ()
Print ( '% S grab a fork' name%)
Print ( '% S grab surface of' name%)
Print ( '% S eat a surface '% name)
the time.sleep (0.1)
Print ('% S placed below the 'name%)
Print ('% S down the fork 'name%)
lock.release ()
lst = [‘alex’,‘wusir’,‘taibai’,‘yuan’]
Thread(target=eat1,args=(lst[0],noodle_lock,fork_lock)).start()
Thread(target=eat2,args=(lst[1],noodle_lock,fork_lock)).start()
Thread(target=eat1,args=(lst[2],noodle_lock,fork_lock)).start()
Thread(target=eat2,args=(lst[3],noodle_lock,fork_lock)).start()
#!/usr/bin/env python
-- coding:utf-8 --
import queue
Thread-safe communication between threads
from queue import Queue # FIFO queue
q = Queue(5)
q.put(0)
q.put(1)
q.put(2)
q.put(3)
q.put(4)
print(‘444444’)
print(q.get())
print(q.get())
print(q.get())
print(q.get())
print(q.get())
Use multiple threads to achieve a requested web page and the web page written document
Producers and consumers to complete the model
5 thread is responsible for the results on the web page request queue
2 thread is responsible for acquiring a Web page code into the file from the queue
from queue import LifoQueue # LIFO queue
last in first out 栈
lfq = LifoQueue(4)
lfq.put(1)
lfq.put(3)
lfq.put(2)
print(lfq.get())
print(lfq.get())
print(lfq.get())
FIFO
# 写一个server,所有的用户的请求放在队列里
# 先来先服务的思想
LIFO
# 算法
Priority queue
# 自动的排序
# 抢票的用户级别 100000 100001
# 告警级别