当前位置: 首页>>代码示例>>Python>>正文


Python Stream._cleanup方法代码示例

本文整理汇总了Python中ws4py.streaming.Stream._cleanup方法的典型用法代码示例。如果您正苦于以下问题:Python Stream._cleanup方法的具体用法?Python Stream._cleanup怎么用?Python Stream._cleanup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ws4py.streaming.Stream的用法示例。


在下文中一共展示了Stream._cleanup方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: WebSocket

# 需要导入模块: from ws4py.streaming import Stream [as 别名]
# 或者: from ws4py.streaming.Stream import _cleanup [as 别名]

#.........这里部分代码省略.........
    def send(self, payload, binary=False):
        """
        Sends the given ``payload`` out.

        If ``payload`` is some bytes or a bytearray,
        then it is sent as a single message not fragmented.

        If ``payload`` is a generator, each chunk is sent as part of
        fragmented message.

        If ``binary`` is set, handles the payload as a binary message.
        """
        message_sender = self.stream.binary_message if binary else self.stream.text_message
        
        if isinstance(payload, basestring) or isinstance(payload, bytearray):
            self.sender(message_sender(payload).single(mask=self.stream.always_mask))

        elif isinstance(payload, Message):
            self.sender(payload.single(mask=self.stream.always_mask))
                
        elif type(payload) == types.GeneratorType:
            bytes = payload.next()
            first = True
            for chunk in payload:
                self.sender(message_sender(bytes).fragment(first=first, mask=self.stream.always_mask))
                bytes = chunk
                first = False

            self.sender(message_sender(bytes).fragment(last=True, mask=self.stream.always_mask))
            
        else:
            raise ValueError("Unsupported type '%s' passed to send()" % type(payload))

    def _cleanup(self):
        """
        Frees up resources used by the endpoint.
        """
        self.sender = None
        self.sock = None
        self.environ = None
        self.stream._cleanup()
        self.stream = None
        
    def run(self):
        """
        Performs the operation of reading from the underlying
        connection in order to feed the stream of bytes.

        We start with a small size of two bytes to be read
        from the connection so that we can quickly parse an
        incoming frame header. Then the stream indicates
        whatever size must be read from the connection since
        it knows the frame payload length.

        Note that we perform some automatic opererations:

        * On a closing message, we respond with a closing
          message and finally close the connection
        * We respond to pings with pong messages.
        * Whenever an error is raised by the stream parsing,
          we initiate the closing of the connection with the
          appropiate error code.

        This method is blocking and should likely be run
        in a thread.
        """
开发者ID:GDur,项目名称:LiveProcessingJs,代码行数:70,代码来源:websocket.py

示例2: WebSocket

# 需要导入模块: from ws4py.streaming import Stream [as 别名]
# 或者: from ws4py.streaming.Stream import _cleanup [as 别名]
class WebSocket(object):
    def __init__(self, sock, handshake_reply, protocols=None):
        self.stream = Stream(always_mask=False)
        self.handshake_reply = handshake_reply
        self.handshake_sent = False
        self.protocols = protocols
        self.sock = sock
        self.client_terminated = False
        self.server_terminated = False
        self.reading_buffer_size = DEFAULT_READING_SIZE
        self.sender = self.sock.sendall

        # This was initially a loop that used callbacks in ws4py
        # Here it was turned into a generator, the callback replaced by yield
        self.runner = self._run()

    def send_handshake(self):
        self.sender(self.handshake_reply)
        self.handshake_sent = True

    def wait(self):
        """
        Reads a message from the websocket, blocking and responding to wire
        messages until one becomes available.
        """
        try:
            return self.runner.next()
        except StopIteration:
            return None

    def send(self, payload, binary=False):
        """
        Sends the given ``payload`` out.

        If ``payload`` is some bytes or a bytearray,
        then it is sent as a single message not fragmented.

        If ``payload`` is a generator, each chunk is sent as part of
        fragmented message.

        If ``binary`` is set, handles the payload as a binary message.
        """
        message_sender = self.stream.binary_message if binary else self.stream.text_message

        if isinstance(payload, basestring) or isinstance(payload, bytearray):
            self.sender(message_sender(payload).single(mask=self.stream.always_mask))

        elif isinstance(payload, Message):
            self.sender(payload.single(mask=self.stream.always_mask))

        elif type(payload) == types.GeneratorType:
            bytes = payload.next()
            first = True
            for chunk in payload:
                self.sender(message_sender(bytes).fragment(first=first, mask=self.stream.always_mask))
                bytes = chunk
                first = False

            self.sender(message_sender(bytes).fragment(last=True, mask=self.stream.always_mask))

        else:
            raise ValueError("Unsupported type '%s' passed to send()" % type(payload))

    def _cleanup(self):
        """
        Frees up resources used by the endpoint.
        """
        self.sender = None
        self.sock = None
        self.stream._cleanup()
        self.stream = None

    def _run(self):
        """
        Performs the operation of reading from the underlying
        connection in order to feed the stream of bytes.

        We start with a small size of two bytes to be read
        from the connection so that we can quickly parse an
        incoming frame header. Then the stream indicates
        whatever size must be read from the connection since
        it knows the frame payload length.

        Note that we perform some automatic operations:

        * On a closing message, we respond with a closing
          message and finally close the connection
        * We respond to pings with pong messages.
        * Whenever an error is raised by the stream parsing,
          we initiate the closing of the connection with the
          appropiate error code.
        """
        self.sock.setblocking(True)
        s = self.stream
        try:
            sock = self.sock

            while not self.terminated:
                bytes = sock.recv(self.reading_buffer_size)
                if not bytes and self.reading_buffer_size > 0:
#.........这里部分代码省略.........
开发者ID:remram44,项目名称:django-websocket,代码行数:103,代码来源:websocket.py


注:本文中的ws4py.streaming.Stream._cleanup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。