day38-thread basis
Acquaintance thread
- In the operating system, each process has an address space, but by default there is a thread of control, cpu real thread execution units. (Like in the factory, every house has a shop, and each shop default there is a pipeline)
- OS ===> Factory
- Process ===> workshop
- Thread ===> Pipeline
- cpu ===> Power
- Thread: cpu smallest unit of execution
- Process: collection / resource unit
- Thread run: Run Code
- Processes running: a variety of resources + thread
Right when you run:
- Will first application memory space, first interpreter throw into the code and also to throw into (the process of things), run the code (threads)
The difference between processes and threads
- The difference between the procedures described
- Thread ==> single finger code execution
- Process ==> application process and the destruction of resources
- Process memory space isolated from each other and share resources with threads in a process
- Create a speed of processes and threads
- Processes need to apply resources to open up space (slow)
- Thread that tells the operating system to perform a program (fast)
Two ways open thread
# 方案一
from threading import Thread
import time
def task():
print('线程 start')
time.sleep(3)
print('线程 end')
if __name__ == '__main__': #线程可以不用这行
t = Thread(target=task)
t.start() #告诉操作系统开一个线程
print('主')
# 补充: 进程会等待所有线程结束才会结束
# 方案二
from threading import Thread
import time
class MyT(Thread):
def run(self):
print('子线程 start')
time.sleep(3)
print('子进程 end')
The child of a child process vs speed comparison
from thteading 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('')
Child threads share resources
from threading import Thead
import time, os
x = 100
def task():
global x
x = 50
print(os.getpid()) #5204(pid)
if __name__ == '__main__':
t = Thread(target=task)
t.start()
time.sleep(2)
print(x) #50
print(os.getpid()) #5204
Thread join method
from threading import Thread
import time
def task():
print('子线程 start')
time.sleep(2)
print('子线程 end')
t = Thread(target=task)
t.start
t.join() #等待子线程运行结束
print('主线程')
#案例1
from threading import Thread
import time
def task(name , n):
print(f'{name} start')
time.sleep(n)
print(f'{name} end')
t1 = Thread(target=task,args=('线程1', 1))
t2 = Thread(target=task,args=('线程2', 2))
t3 = Thread(target=task,args=('线程3', 3))
start = time.time()
t1.start
t2.start
t3.start
t1.join() #等待子线程运行结束
t2.join()
t3.join()
end = time.time()
print(end-start) #阻塞时间为3点多秒
print('主线程')
#案例二(了解部分)
from multiprocessing import Process
from threading import Thread
import time
def task():
print('进程 开启') #3
time.sleep(10)
print('进程 结束') #5
def task2():
print('子线程 开启') #1
time.sleep(2)
print('子线程 结束') #4
if __name__ == '__main__':
p = Process(target=task)
t = Thread(target=task2)
t.start() #开线程
p.start() #开进程
print('子进程join开始') #2
p.join() #主进程的主线程等待子进程运行结束
print('主') #6
#结果
子线程 开启
子进程join开始
进程 开启
子线程 结束
进程 结束
主
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('主')
Other uses of the Thread class
Thread instantiated object:
isAlive(): 返回线程是否活动的
getName(): 返回线程名字
setName(): 设置
Some methods provided by threading module:
threading.currentThead(): 返回当前的线程变量
threading.enumerate(): 返回一个包含正在运行的线程list, 正在运行指线程启动后,结束前,不包含启动前和终止后线程
threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果