0

クラスに単純な epoll Web サーバーを実装しようとしています。大きなファイルを送信しようとするまで、すべてがうまく機能します。データの約 2/3 を送信し、そこに座って何もしないことに気付きました。これは、すべてのデータが確実に送信されるようにする方法です。

def sendResponse(self,fd,response):
    if response:
        totalsent = 0
        MSGLEN = len(response)
        while totalsent < MSGLEN:
            sent = self.clients[fd].send(response[totalsent:])
            if sent == 0:
                raise RuntimeError("socket connection broken")
            totalsent = totalsent + sent
    else:
        self.poller.unregister(fd)
        self.clients[fd].close()
        del self.clients[fd]

これは、小規模から中規模のファイルで機能することに言及しましたか。1.7 Mbs 以上のファイルを送信しようとすると壊れることに気付きました。

4

1 に答える 1

0

可能であれば、車輪の再発明のフェーズをスキップして、ZeroMQ メッセージング レイヤーを使用してください。

このようにして、コードはすべての低レベルの重要事項を忘れて、開発時間と焦点が問題ドメインの問題に専念する可能性があります。ZeroMQ は、(ほぼ) 線形のスケーラビリティ、リソース プール、障害回復力、上位層の抽象プロトコルなどを追加するための概念と一連の既製のツールの両方を提供します。

最初のケーキとして、ニコラス・ピエルからの眺めを味わってください

Pieters Hintjens の著書「Code Connected, Vol.1」を 1 週間読むと、すぐに使える分散処理の新しい世界が紹介され、The Masters のノウハウを再利用する準備が整います。

あなたは何を得ますか?

数バイトまたは数 GB の BLOB、低レイテンシなど、すべてが次のような単純なコードから、悪魔のように高速に処理されます。

def sendResponse( self, aZmqConnectionToCounterparty, response ):
     if response:
          try:
              aZmqConnectionToCounterparty.send( response, zmq.NOBLOCK )
          except ZMQerror:
              ''' handle ZMQerror   '''
          except ValueError:
              ''' handle ValueError '''
          except TypeError:
              ''' handle TypeError  '''
    ...

PyZMQ のドキュメントは次のとおりです。

.send( data, flags = 0, copy = True, track = False )
Send a message on this socket.

This queues the message to be sent by the IO thread at a later time.

Parameters:
-----------
data:   object, str, Frame    The content of the message.
flags:  int                   Any supported flag: NOBLOCK, SNDMORE.
copy:   bool                  Should the message be sent in a copying or non-copying manner.
track:  bool                  Should the message be tracked for notification
                              that ZMQ has finished with it? ( ignored if copy = True )

Returns:
--------
None:   if copy or not track

None if message was sent, raises an exception otherwise.

MessageTracker: if track and not copy

a MessageTracker object, whose pending property will be True until the send is completed.

Raises:
-------
TypeError                    If a unicode object is passed
ValueError                   If track=True, but an untracked Frame is passed.
ZMQError                     If the send does not succeed for any reason.
于 2014-10-29T05:33:57.793 に答える