Title: How to rise max rate on socketserver of Python3?
Tags: python-3.x,sockets,dns,restriction,socketserver

Question: Im working at Python3 DNS server, and i ran into a problem with max queries per second using socketserver module.
There is statistic for classic Bind9 server:
    DNS Performance Testing Tool
    Version 2.11.2
    
    [Status] Command line: dnsperf -s 127.0.0.1 -d example.com -l 60
    [Status] Sending queries (to 127.0.0.1:53)
    [Status] Started at: Mon May  8 14:26:55 2023
    [Status] Stopping after 60.000000 seconds
    [Status] Testing complete (time limit)
    
    Statistics:
    
    Queries sent:         3055286
    Queries completed:    3055286 (100.00%)
    Queries lost:         0 (0.00%)
    
    Response codes:       NOERROR 3055286 (100.00%)
    Average packet size:  request 29, response 45
    Run time (s):         60.010743
    Queries per second:   50912.317483
    
    Average Latency (s):  0.001872 (min 0.000050, max 0.077585)
    Latency StdDev (s):   0.000859
    
    **vic@waramik:/home/vic/scripts$ uptime**
    14:27:58 up 2 days,  4:09,  1 user,  load average: 0.73, 0.29, 0.25

As you can see rate is about 50k q/s with LA for 1m is 0.73 of 2 cores.
And there is echo DNS server made on Python3 with socketserver module:
    import socketserver
    
    class UDPserver(socketserver.BaseRequestHandler):
    
        def handle(self):
            data, sock = self.request
            sock.sendto(data, self.client_address)
    
    if __name__ == "__main__":
    host = "127.0.0.2"
    port = 53
    addr = (host, port)
    with socketserver.ThreadingUDPServer(addr, UDPserver) as udp:
        print(f'Start to listen on {addr}')
        udp.serve_forever(0.1)

Which will do something like this:
    $ dig example.com @127.0.0.2
    ;; Warning: query response not set
    
    ; \<\<\>\> DiG 9.18.12-0ubuntu0.22.04.1-Ubuntu \<\<\>\> example.com @127.0.0.2
    ;; global options: +cmd
    ;; Got answer:
    ;; -\>\>HEADER\<\<- opcode: QUERY, status: NOERROR, id: 10796
    ;; flags: rd ad; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1
    ;; WARNING: recursion requested but not available
    
    ;; OPT PSEUDOSECTION:
    ; EDNS: version: 0, flags:; udp: 1232
    ; COOKIE: 12158dbef76fddc9 (echoed)
    ;; QUESTION SECTION:
    ;example.com.                   IN      A
    
    ;; Query time: 0 msec
    ;; SERVER: 127.0.0.2#53(127.0.0.2) (UDP)
    ;; WHEN: Mon May 08 14:24:33 MSK 2023
    ;; MSG SIZE  rcvd: 52

And there is statistic after PyDNS server's stress test:
    DNS Performance Testing Tool
    Version 2.11.2
    
    [Status] Command line: dnsperf -s 127.0.0.2 -d example.com -l 60
    [Status] Sending queries (to 127.0.0.2:53)
    [Status] Started at: Mon May  8 14:29:35 2023
    [Status] Stopping after 60.000000 seconds
    [Status] Testing complete (time limit)
    
    Statistics:
    
    Queries sent:         478089
    Queries completed:    478089 (100.00%)
    Queries lost:         0 (0.00%)
    
    Response codes:       NOERROR 478089 (100.00%)
    Average packet size:  request 29, response 29
    Run time (s):         60.024616
    Queries per second:   7964.882274
    
    Average Latency (s):  0.012543 (min 0.000420, max 0.082480)
    Latency StdDev (s):   0.003576
    
    $ uptime
    14:30:49 up 2 days,  4:12,  1 user,  load average: 1.22, 0.56, 0.34

As you can see statistic performance is bordered at ~8k q/s with LA for 1m is 1.22 of 2 cores.
Can you advise what im doing wrong?
Before i tried to use a bundle of socket and threading modules and max q/s was at ~5k.
Also i have read many similar examples and dont found solutions.

Answer: Well i found a solution.
I have replaced socketserver on asincio Datagram protocol and it is working now