python全栈脱产第37天------进程池与线程池、协程、gevent模块、单线程下实现并发的套接字通信
2021-07-03 14:06
标签:add 客户 targe star rom cli nic target and 一、进程池与线程池 调用concurrent.futures下的ThreadPoolExecutor,ProcessPoolExecutor来实现 提交任务有两种方式:同步调用:提交完一个任务之后,就在原地等待,等待任务完完整整地运行完毕拿到结果后,在执行下一段代码,是串行的 异步调用:提交完一个任务之后,不在原地等待,直接运行下一段代码,任务是并发的 def task(name,n): if __name__ == ‘__main__‘: # 异步提交 p.shutdown(wait=True) def play(name): def eat(name): # g1.join() def comunicate(conn): while True: # 链接循环 # 通信 if __name__ == ‘__main__‘: def client(): n=0 if __name__ == ‘__main__‘: python全栈脱产第37天------进程池与线程池、协程、gevent模块、单线程下实现并发的套接字通信 标签:add 客户 targe star rom cli nic target and 原文地址:https://www.cnblogs.com/zhouyi0316/p/9622522.htmlfrom concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import time,random,os
print(‘%s%s is running‘ %(name,os.getpid()))
time.sleep(random.randint(1,3))
return n**2
# print(os.cpu_count())
p=ProcessPoolExecutor(4) l=[]
for i in range(10):
# 同步提交
# res=p.submit(task,‘进程pid: ‘,i).result()
# print(res)
future=p.submit(task,‘进程pid: ‘,i)
l.append(future) for future in l:
print(future.result())
print(‘主‘)
二、协程
目标:在线程下实现并发:切换+保存状态
定义:协程就是单线程实现并发
注意:协程是程序员意淫出来的东西,操作系统中只有进程和线程的概念(操作系统调度的是线程)
用途:在单线程下实现多个任务间遇到IO操作切换就可以降低单线程的IO时间,从而最大限度地提升单线程的效率
三、gevent模块
from gevent import monkey;monkey.patch_all()
from gevent import spawn,joinall #pip3 install gevent
import time
print(‘%s play 1‘ %name)
time.sleep(5)
print(‘%s play 2‘ %name)
print(‘%s eat 1‘ %name)
time.sleep(3)
print(‘%s eat 2‘ %name)
start=time.time()
g1=spawn(play,‘XXX‘)
g2=spawn(eat,‘XXX‘)
# g2.join()
joinall([g1,g2])
print(‘主‘,time.time()-start)
四、单线程下实现并发的套接字通信
服务端
from gevent import monkey;monkey.patch_all()
from socket import *
from gevent import spawn
while True: # 通信循环
try:
data = conn.recv(1024)
if len(data) == 0: break
conn.send(data.upper())
except ConnectionResetError:
break
conn.close()
def server(ip, port, backlog=5):
server = socket(AF_INET, SOCK_STREAM)
server.bind((ip, port))
server.listen(backlog)
conn, client_addr = server.accept()
print(client_addr)
spawn(comunicate,conn)
g1=spawn(server,‘127.0.0.1‘,8080)
g1.join()
客户端from threading import Thread,current_thread
from socket import *
client=socket(AF_INET,SOCK_STREAM)
client.connect((‘127.0.0.1‘,8080))
while True:
msg=‘%s say hello %s‘ %(current_thread().name,n)
n+=1
client.send(msg.encode(‘utf-8‘))
data=client.recv(1024)
print(data.decode(‘utf-8‘))
for i in range(500):
t=Thread(target=client)
t.start()
上一篇:c++存储区域
下一篇:JavaSE——集合框架
文章标题:python全栈脱产第37天------进程池与线程池、协程、gevent模块、单线程下实现并发的套接字通信
文章链接:http://soscw.com/essay/101297.html