python3通过gevent.pool限制协程并发数量的实现方法
协程虽然是轻量级的线程,但到达一定数量后,仍然会造成服务器崩溃出错。最好的方法通过限制协程并发数量来解决此类问题。
server代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : Cain
# @Email : 771535427@qq.com
# @Filename : gevnt_sockserver.py
# @Last modified : 2017-11-24 16:31
# @Description :
import sys
import socket
import time
import gevent
from gevent import socket,monkey,pool #导入pool
monkey.patch_all()
def server(port, pool):
s = socket.socket()
s.bind(('0.0.0.0', port))
s.listen()
while True:
cli, addr = s.accept()
#print("Welcome %s to SocketServer" % str(addr[0]))
pool.spawn(handle_request, cli) #通过pool.spawn()运行协程
def handle_request(conn):
try:
data = conn.recv(1024)
print("recv:", data)
data = 'From SockeServer:192.168.88.118---%s' % data.decode("utf8")
conn.sendall(bytes(data, encoding="utf8"))
if not data:
conn.shutdown(socket.SHUT_WR)
except Exception as ex:
print(ex)
finally:
conn.close()
if __name__ == '__main__':
pool = pool.Pool(5) #限制并发协程数量5
server(8888, pool)
client(通过gevent模拟并发数量):
import socket
import gevent
from gevent import socket, monkey
from gevent.pool import Pool
import time
monkey.patch_all()
HOST = '192.168.88.118'
PORT = 8888
def sockclient(i):
#time.sleep(2)
s = socket.socket()
s.connect((HOST, PORT))
#print(gevent.getcurrent())
msg = bytes(("This is gevent: %s" % i),encoding="utf8")
s.sendall(msg)
data = s.recv(1024)
print("Received", data.decode())
s.close()
pool = Pool(5)
threads = [pool.spawn(sockclient, i) for i in range(2000)]
gevent.joinall(threads)
由于服务器限制连接并发数量;所以客户端同时并发连接数超过服务器端并发数量,就会引发连接错误信息:
Exception in thread Thread-849:
Traceback (most recent call last):
File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "E:/chengd/python/python3/matp/die/geven_sockclient.py", line 26, in sockclient
data = s.recv(1024)
ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
到此这篇关于python3通过gevent.pool限制协程并发数量的文章就介绍到这了,更多相关python3协程并发数量内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341