协程
我们都知道进程是资源单位
线程是执行单位
协程是单线程下实现并发(效率极高)
在代码层面欺骗CPU 让CPU觉得我们的代码里面没有IO操作
实际上IO操作被我们自己写的代码检测 一旦有 立刻让代码执行别的
(该技术完全是程序员自己弄出来的 名字也是程序员自己起的)
核心:自己写代码完成切换+保存状态
代码展示
import time
from gevent import monkey;
monkey.patch_all()
from gevent import spawn
def func1():
print('func1 running')
time.sleep(3)
print('func1 over')
def func2():
print('func2 running')
time.sleep(5)
print('func2 over')
if __name__ == '__main__':
start_time = time.time()
s1 = spawn(func1)
s2 = spawn(func2)
s1.join()
s2.join()
print(time.time() - start_time)
协程实现TCP服务端并发
'服务端'
import socket
from gevent import monkey;
monkey.patch_all()
from gevent import spawn
def communication(sock):
while True:
data = sock.recv(1024)
print(data.decode('utf8'))
sock.send(data.upper())
def get_server():
server = socket.socket()
server.bind(('127.0.0.1', 8080))
server.listen(5)
while True:
sock, addr = server.accept()
spawn(communication, sock)
s1 = spawn(get_server)
s1.join()
'客户端'
import socket
from threading import Thread, current_thread
def get_client():
client = socket.socket()
client.connect(('127.0.0.1', 8080))
while True:
client.send(f'hello baby {current_thread().name}'.encode('utf8'))
data = client.recv(1024)
print(data.decode('utf8'))
for i in range(1000):
t = Thread(target=get_client)
t.start()