3

Twisted ベースのサーバーを作成しましたが、Twisted を使用してテストしたいと考えています。

しかし、同時に大量のリクエストを開始する負荷テストを書きたいと思います。

しかし、この問題で立ち往生しているため、主にクライアント側の Twisted の概念を理解していないと思います。

    from twisted.internet import reactor, protocol
from threading import Thread
from twisted.protocols.basic import LineReceiver

__author__="smota"
__date__ ="$30/10/2009 17:17:50$"

class SquitterClient(LineReceiver):

    def connectionMade(self):
        self.sendLine("message from " % threading.current_thread().name);
        pass

    def connectionLost(self, reason):
        print "connection lost"

    def sendMessage(self, msg):
        for m in [ "a", "b", "c", "d", "e"]:
            self.sendLine(msg % " - " % m);

class SquitterClientFactory(protocol.ClientFactory):
    protocol = SquitterClient

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed - goodbye!"
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print "Connection lost - goodbye!"
        reactor.stop()

def createAndRun():
    f = SquitterClientFactory()
    reactor.connectTCP("localhost", 4010, f)
    reactor.run(installSignalHandlers=0)

# this connects the protocol to a server runing on port 8000
def main():
    for n in range(0,10):
        th=Thread(target=createAndRun)
        th.start()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

socket_client.py:35: DeprecationWarning: Reactor はすでに実行中です! この動作は Twisted 8.0 で非推奨になりました

私は何が欠けていますか?

それをテストする方法は?

ありがとうございました、

サミュエル

4

1 に答える 1

9

失敗の直接的な原因は、reactor で run() を複数回呼び出そうとしたことです。run() は 1 回だけ呼び出す必要があります。それぞれが独自のスレッドに複数のリアクターがあることを期待していると思いますが、実際には1つしかありません。悪い点は、複数の原子炉を持つことが困難または不可能であることです。良い点は、それが不要であることです。実際、複数のスレッドは必要ありません。複数の接続をリッスンするのと同じくらい簡単に、1 つのリアクターで複数のクライアント接続を多重化できます。

サンプル コードを変更すると、次のように動作するはずです。重要なアイデアは、同時に処理を行うために複数のリアクターを必要としないということです。とにかく、通常の Python 実装と並行できる唯一のものは I/O です。

from twisted.internet import reactor, protocol
from twisted.protocols.basic import LineReceiver

__author__="smota"
__date__ ="$30/10/2009 17:17:50$"

class SquitterClient(LineReceiver):
    def connectionMade(self):
        self.messageCount = 0
        # The factory provides a reference to itself, we'll use it to enumerate the clients
        self.factory.n += 1
        self.name = "Client %d" %self.factory.n

        # Send initial message, and more messages a bit later
        self.sendLine("Client %s starting!" % self.name);
        reactor.callLater(0.5, self.sendMessage, "Message %d" %self.messageCount)

    def connectionLost(self, reason):
        print "connection lost"

    def sendMessage(self, msg):
        for m in [ "a", "b", "c", "d", "e"]:
            self.sendLine("Copy %s of message %s from client %s!" % (m, msg, self.name))
        if self.factory.stop:
            self.sendLine("Client %s disconnecting!" % self.name)
            self.transport.loseConnection()
        else:
            self.messageCount += 1
            reactor.callLater(0.5, self.sendMessage, "Message %d" %self.messageCount)

class SquitterClientFactory(protocol.ClientFactory):
    protocol = SquitterClient

    def __init__(self):
        self.n = 0
        self.stop = False

    def stopTest():
        self.stop = True

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed - goodbye!"

    def clientConnectionLost(self, connector, reason):
        print "Connection lost - goodbye!"

# this connects the protocol to a server running on port 8000
def main():
    # Create 10 clients

    f = SquitterClientFactory()
    for i in range(10):
        reactor.connectTCP("localhost", 8000, f)

    # Schedule end of test in 10 seconds
    reactor.callLater(10, f.stopTest)

    # And let loose the dogs of war
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()
于 2009-11-01T12:47:18.757 に答える