前奏知识
进程与线程
"""
进程:资源单位
线程:执行单位
进程把线程执行过程中所需的资源集中到一起,每一个进程内都有一个线程,一个进程内可以有多个线程。
"""
开启线程的两种方式
方式一
from threading import Thread
import os
import random
import time
def server(name):
print(f'服务工号:[{os.getpid()}],技师[{name}]已上线!')
time.sleep(random.randrange(1, 3))
if __name__ == '__main__':
t = Thread(target=server, args=('egon',))
t.start()
print(f'{os.getpid()}')
"""
服务工号:[44599],技师[egon]已上线!
44599
"""
方式二
from threading import Thread
import random
import time
import os
class MySever(Thread):
def __init__(self, name):
super().__init__()
self.name = name
def run(self):
print(f'服务工号:[{os.getpid()}],技师[{self.name}]已上线!')
time.sleep(random.randrange(1, 3))
if __name__ == '__main__':
t = MySever('jason')
t.start()
print(f'{os.getpid()}')
TCP服务端实现并发的效果
客户端
import socket
IP_ADDRESS = ('182.92.59.34', 9090)
BUF_SIZE = 1024
class MyClient:
def __init__(self, ip_address, buf_size):
self.ip_address = ip_address
self.buf_size = buf_size
self.client = socket.socket()
def connect(self):
connect_err = None
try:
self.client.connect(IP_ADDRESS)
except Exception as e:
connect_err = e
return connect_err
def disconnect(self):
self.client.close()
def run(self):
connect_err = self.connect()
if connect_err:
print(f'connect error:{connect_err}')
while 1:
msg = input('请输入要发送的消息:').strip()
if not msg: continue
self.client.send(msg.encode('utf-8'))
back_msg = self.client.recv(self.buf_size).decode('utf-8')
print(f'收到消息:')
_continue = input('是否继续发送消息(y,n):').strip().lower()
if _continue == 'n': break
self.disconnect()
if __name__ == '__main__':
client = MyClient(IP_ADDRESS, BUF_SIZE)
client.run()
服务端
import socket
from threading import Thread
IP_ADDRESS = ('0.0.0.0', 9090)
BUF_SIZE = 1024
class MyServer:
def __init__(self, ip_address, buf_size):
self.ip_address = ip_address
self.buf_size = buf_size
self.server = socket.socket()
def bind(self):
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind(self.ip_address)
def listen(self):
self.server.listen(5)
def accept(self):
conn, addr = self.server.accept()
print(f'接到来自[{addr[0]}:{addr[1]}]的消息!')
return conn
def communicate(self, conn):
while 1:
try:
data = conn.recv(self.buf_size)
if not data: break
print(f"收到消息:{data.decode('utf-8')}")
back_msg = conn.send('egon一秒五'.encode('utf-8'))
except ConnectionResetError as e:
print(f'connect error:{e}')
break
conn.close()
def run(self):
self.bind()
self.listen()
while 1:
conn = self.accept()
t = Thread(target=self.communicate, args=(conn,))
t.start()
if __name__ == '__main__':
server = MyServer(IP_ADDRESS, BUF_SIZE)
server.run()
效果:
"""
[root@alisurpass project]# python server.py
接到来自[49.85.186.112:5683]的消息!
接到来自[49.85.186.112:5685]的消息!
收到消息:1号技师姜春上线
接到来自[49.85.186.112:5700]的消息!
收到消息:2号技师李乾新上线
"""
小练习
"""
三个任务,一个接收用户输入,一个将用户输入的内容格式化成大写,一个将格式化后的结果存入文件
"""
示例
from threading import Thread
import os
FILE_PATH = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'message.txt'
)
msg_lst = []
format_lst = []
def recv_user_enter():
while 1:
msg = input('请输入消息:').strip()
if not msg: continue
msg_lst.append(msg)
def format_msg():
while 1:
if msg_lst:
res = msg_lst.pop()
format_lst.append(res.upper())
def save():
while 1:
if format_lst:
with open(FILE_PATH, mode='ab') as fw:
res = format_lst.pop()
fw.write(res.encode('utf-8'))
if __name__ == '__main__':
t1 = Thread(target=recv_user_enter)
t2 = Thread(target=format_msg)
t3 = Thread(target=save)
t1.start()
t2.start()
t3.start()
线程的相关方法
Thread实例对象的方法
"""
isAlive() 返回线程是否是活动的
getName() 返回线程名
setName() 设置线程名
"""
threading模块提供的一些其它方法:
"""
threading.currentThread() 返回当前的线程变量
threading.enumerate() 返回一个包含正在运行线程的list
threading.activeCount() 返回正在运行的线程数量
"""
示例:
from threading import Thread
import threading
import random
import time
import os
def beast(name):
print(f'技师[{name}]已上线!')
time.sleep(random.randrange(1, 3))
print(threading.current_thread().getName())
if __name__ == '__main__':
t = Thread(target=beast, args=('jason',))
t.start()
print(threading.current_thread()) # 主线程
print(threading.current_thread().getName())
print(threading.enumerate())
print(threading.active_count())
print('主线程/主线程')
线程对象的join方法
from threading import Thread
import time
def task(name):
print('%s is running'%name)
time.sleep(3)
print('%s is over'%name)
if __name__ == '__main__':
t = Thread(target=task,args=('egon',))
t.start()
t.join() # 主线程等待子线程运行结束再执行
print('主')
同一进程下的多个进程数据是共享的
from threading import Thread
import time
money = 100
def task():
global money
money = 666
print(money)
if __name__ == '__main__':
t = Thread(target=task)
t.start()
t.join()
print(money)
守护线程
无论是进程还是线程,都遵循:守护xxx会等待主xxx运行完毕后被销毁
示例:
宫女【jason】和总管【egon】互相深爱着对方,【egon】默默守护着【jason】。然而,在皇宫这个吃人不吐骨头的地方,人是不能有感情的。某年某月某日,皇帝【tank】看上【jason】,欲纳【jason】为妃。自古,忠爱两难全,【jason】饮下鸩酒,一命呜呼,【egon】也随之而去。
代码:
from threading import Thread
import time
import random
def beast(name):
print(f'总管[{name}]已上线')
time.sleep(random.randrange(1, 3))
print(f'总管[{name}]殉情自杀')
if __name__ == '__main__':
t = Thread(target=beast, args=('egon',))
t.daemon = True
t.start()
print('宫女[jason]饮鸩酒而亡')
"""
总管[egon]已上线
宫女[jason]饮鸩酒而亡
"""
一个具有迷惑性的例子
from threading import Thread
import time
def foo():
print(123)
time.sleep(1)
print('end123')
def func():
print(456)
time.sleep(3)
print('end456')
if __name__ == '__main__':
t1 = Thread(target=foo)
t2 = Thread(target=func)
t1.daemon = True
t1.start()
t2.start()
print('主.......')
"""
123
456
主
end123
end456
"""
线程互斥锁
from threading import Thread,Lock
import time
money = 100
mutex = Lock()
def task():
global money
mutex.acquire()
tmp = money
time.sleep(0.1)
money = tmp - 1
mutex.release()
if __name__ == '__main__':
t_list = []
for i in range(100):
t = Thread(target=task)
t.start()
t_list.append(t)
for t in t_list:
t.join()
print(money)
GIL全局解释器锁(Global Interpreter Lock )
"""
In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple
native threads from executing Python bytecodes at once. This lock is necessary mainly
because CPython’s memory management is not thread-safe. (However, since the GIL
exists, other features have grown to depend on the guarantees that it enforces.)
"""
在CPython解释器中GIL是一把互斥锁,用来阻止同一个进程下的多个线程的同时执行,进而同一个进程下的多个线程无法利用多核优势。
因为CPython中的内存管理不是线程安全的
垃圾回收机制(GC)
"""
1.引用计数
2.标记清除
3.分代回收
"""
"""
重点:
1.GIL不是python的特点而是CPython解释器的特点
2.GIL是保证解释器级别的数据的安全
3.GIL会导致同一个进程下的多个线程的无法同时执行即无法利用多核优势(******)
4.针对不同的数据还是需要加不同的锁处理
5.解释型语言的通病:同一个进程下多个线程无法利用多核优势
"""
GIL与普通互斥锁的区别
from threading import Thread,Lock
import time
mutex = Lock()
money = 100
def task():
global money
# with mutex:
# tmp = money
# time.sleep(0.1)
# money = tmp -1
mutex.acquire()
tmp = money
time.sleep(0.1) # 只要你进入IO了 GIL会自动释放
money = tmp - 1
mutex.release()
if __name__ == '__main__':
t_list = []
for i in range(100):
t = Thread(target=task)
t.start()
t_list.append(t)
for t in t_list:
t.join()
print(money)
"""
100个线程起起来之后 要先去抢GIL
我进入io GIL自动释放 但是我手上还有一个自己的互斥锁
其他线程虽然抢到了GIL但是抢不到互斥锁
最终GIL还是回到你的手上 你去操作数据
"""
同一个进程下的多线程无法利用多核优势?
"""
多线程是否有用要看具体情况
单核:四个任务(IO密集型计算密集型)
多核:四个任务(IO密集型计算密集型)
"""
计算密集型 每个任务都需要10s
"""
多核:
多进程:10+
多线程:40+
"""
I/O密集型
"""
多进程:相对浪费资源
多线程:更加节省资源
"""
代码验证
# 计算密集型
# from multiprocessing import Process
# from threading import Thread
# import os,time
#
#
# def work():
# res = 0
# for i in range(10000000):
# res *= i
#
# if __name__ == '__main__':
# l = []
# print(os.cpu_count()) # 获取当前计算机CPU个数
# start_time = time.time()
# for i in range(12):
# p = Process(target=work) # 1.4679949283599854
# t = Thread(target=work) # 5.698534250259399
# t.start()
# # p.start()
# # l.append(p)
# l.append(t)
# for p in l:
# p.join()
# print(time.time()-start_time)
# IO密集型
from multiprocessing import Process
from threading import Thread
import os,time
def work():
time.sleep(2)
if __name__ == '__main__':
l = []
print(os.cpu_count()) # 获取当前计算机CPU个数
start_time = time.time()
for i in range(4000):
# p = Process(target=work) # 21.149890184402466
t = Thread(target=work) # 3.007986068725586
t.start()
# p.start()
# l.append(p)
l.append(t)
for p in l:
p.join()
print(time.time()-start_time)
总结:
"""
多进程和多线程都有各自的优势
并且我们后面在写项目的时候通常可以多进程下面再开设多线程
这样的话既可以利用多核也可以介绍资源消耗
"""