Realization method of python3 limiting the number of concurrent processes through gevent. pool

  • 2021-11-29 07:54:12
  • OfStack

Co-process is a lightweight thread, but after reaching a certain number, it will still cause server crash and error. The best way to solve this problem is to limit the number of co-concurrency.

server code:


#!/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    # Import 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)    # Pass pool.spawn() Operation coordination process 

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)    # Limit the number of concurrent coprocesses 5
    server(8888, pool)

client (gevent simulates the number of concurrency):


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)

Because the server limits the number of concurrent connections; Therefore, if the number of concurrent connections on the client side exceeds the number of concurrent connections on the server side, a connection error message will be raised:

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] The remote host forced an existing connection to close.


Related articles: