簡體   English   中英

Stackless python網絡性能隨着時間的推移而降低?

[英]Stackless python network performance degrading over time?

所以我正在玩無堆棧的python,編寫一個非常簡單的網絡服務器來教我自己用微線程/ tasklet編程。 但是現在我的問題是,當我在apache工作台上運行類似ab -n 100000 -c 50 http://192.168.0.192/請求,50並發)的東西時,我得到類似6k req / s的東西,這是我第二次運行我得到5.5k,第三次5k,第四次,4.5k等,一直到100req / s或者什么的。

但是,當我重新啟動python腳本時問題就消失了。

現在我的問題是為什么? 我忘記刪除tasklets了嗎? 我已經檢查了stackless.getrunco​​unt()(由於某種原因它似乎總是返回1)所以它似乎不會有任何死的tasklet掛在身邊? 我試過在所有完成的tasklet上調用.kill(),沒有幫助。 我只是想不出這個。

import socket
import select
import stackless
import time

class socket_wrapper(object):
    def __init__(self, sock, sockets):
        super(socket_wrapper, self).__init__()
        self.sock = sock
        self.fileno = sock.fileno
        self.sockets_list = sockets
        self.channel = stackless.channel()
        self.writable = False
        self.error = False

    def remove(self):
        self.sock.close()
        self.sockets_list.remove(self)

    def send(self, data):
        self.sock.send(data)

    def push(self, bytes):
        self.channel.send(self.sock.recv(bytes))

def stackless_accept(accept, handler, recv_size=1024, timeout=0):
    sockets = [accept]

    while True:
        read, write, error = select.select(sockets, sockets, sockets, timeout)

        for sock in read:
            if sock is accept:
                # Accept socket and create wrapper
                sock = socket_wrapper(sock.accept()[0], sockets)

                # Create tasklett for this connection
                tasklet = stackless.tasklet(handler)
                tasklet.setup(sock)

                # Store socket
                sockets.append(sock)

            else:
                # Send data to handler
                sock.push(recv_size)

        # Tag all writable sockets
        for sock in write:
            if sock is not accept:
                sock.writable = True

        # Tag all faulty sockets
        for sock in error:
            if sock is not accept:
                sock.error = True
            else:
                pass # should do something here if the main socket is faulty

        timeout = 0 if socket else 1
        stackless.schedule() 

def simple_handler(tsock):
    data = ""

    while data[-4:] != "\r\n\r\n":
        data += tsock.channel.receive()

    while not tsock.writable and not tsock.error:
        stackless.schedule()

    if not tsock.error:
        tsock.send("HTTP/1.1 200 OK\r\nContent-length: 8\r\n\r\nHi there")
        tsock.remove()

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("192.168.0.192", 8000))
sock.listen(5)

stackless.tasklet(stackless_accept)(sock, simple_handler)
stackless.run()

兩件事情。

首先,請使用大寫字母開頭。 它更傳統,更容易閱讀。

更重要的是,在stackless_accept函數中,您將累積一個名為socketsSock對象list 這份名單似乎無休止地增長。 是的,你有一個remove ,但並不總是調用它。 如果套接字出錯,那么它似乎將永遠留在集合中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM