python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python3协程并发数量

python3通过gevent.pool限制协程并发数量的实现方法

作者:chengd

这篇文章主要介绍了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协程并发数量内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文