table of Contents
Thread
What is the thread
- Is a unit within the basic unit of execution flow time resources allocated to the processor system, or a process performed independent
Feature
- The smallest unit of program execution
Why do we need a thread
- At the same time the process can only do one thing, if you want to do a lot of things, it will be difficult
- Encountered in the implementation process blocked, then the whole process will hang
How to open thread
#方式一
from threading import Thread
import time
###线程开启的第一种方式
def mythred():
print('线程开启')
time.sleep(5)
print('线程结束')
#线程中可加可不加
if __name__ == '__main__':
t = Thread(target = mythred)
t.start()
#####方式二
from threading import Thread
import time
#通过类继承额方法
class Mythred(Thread):
def run(self):
print('线程开启')
time.sleep(5)
print('线程结束')
t = Mythred()
t.start()
Thread creation rate vs speed the process of creating
from threading import Thread
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(2)
print(f'{name} is end')
if __name__ == '__main__':
t = Thread(target=task,args=('子线程',))
p = Process(target=task,args=('子进程',))
# t.start()
p.start()
print('主')
'''
开启子线程的打印效果:
子线程 is running
主
子线程 is end
开启子进程打印效果:
主
子进程 is running
子进程 is end
进程和线程的创建速度
开启子进程需要申请资源开辟空间 慢
开启子线程只是告诉操作系统一个执行方案 快
'''
join method thread
Join the same method join method to use threads and processes
from threading import Thread
import time
def task():
print('子线程 start')
time.sleep(2)
print('子线程 end')
t = Thread(target=task)
t.start()
t.join() # 等待子线程运行结束
print('主线程')
Daemon thread
# 守护线程 守护的是进程的运行周期
from threading import Thread,enumerate,currentThread
import time
def task():
print('守护线程开始')
print(currentThread())
time.sleep(20)
# print('守护线程结束')
def task2():
print('子线程 start')
time.sleep(5)
print(enumerate())
print('子线程 end')
if __name__ == '__main__':
t1 = Thread(target=task)
t2 = Thread(target=task2)
t1.daemon = True
t2.start()
t1.start()
print('主')
Security thread lock
from threading import Thread,Lock
x = 0
def task():
global x
for i in range(100000):
x += 1
t1 = Thread(target=task)
t2 = Thread(target=task)
t3 = Thread(target=task)
t1.start()
t2.start()
t3.start()
print(x)
228891
We found that the above code, this should occur a deviation of the value of x 300,000.
Solution
from threading import Thread,Lock
x = 0
lock = Lock()
def task():
global x
lock.acquire()
for i in range(200000):
x += 1
lock.release()
t1 = Thread(target=task)
t2 = Thread(target=task)
t3 = Thread(target=task)
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
print(x)
Thread deadlock issues
The so-called deadlock
- Phenomenon refers to two or more processes or threads in the implementation process, a result of competition for resources caused by waiting for each other, in the absence of external force, they will not be able to promote it. At this time, say the system is in deadlock state or system to produce a deadlock, which is always in the process of waiting for another process called the deadlock