當前位置: 首頁>>代碼示例>>Python>>正文


Python Channel.unicast_tcp方法代碼示例

本文整理匯總了Python中channel.Channel.unicast_tcp方法的典型用法代碼示例。如果您正苦於以下問題:Python Channel.unicast_tcp方法的具體用法?Python Channel.unicast_tcp怎麽用?Python Channel.unicast_tcp使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在channel.Channel的用法示例。


在下文中一共展示了Channel.unicast_tcp方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Client

# 需要導入模塊: from channel import Channel [as 別名]
# 或者: from channel.Channel import unicast_tcp [as 別名]

#.........這裏部分代碼省略.........
                self.socket = None
                with self.socket_status.get_lock():
                    self.socket_status.value = 0
                continue
            break
        if self.socket is None:
            print('could not open socket')
            sys.exit(1)

        # init a unicast channel with delayed function, reuse code from MP1
        self.channel = Channel(self, self.client_id, self.socket, self.process_info, self.addr_dict)

    def run(self):
        try:
            while True:
                data, address = self.socket.recvfrom(4096)
                # print("receive server message: ", data, address)

                # break if connected server crashed
                if not data:
                    break

                self.channel.recv(data)
        except:
            print("run() Exceptions")
        finally:
            print("connected server crash")
            if self.socket:
                self.socket.close()
                self.socket = None
                with self.socket_status.get_lock():
                    self.socket_status.value = 0  # set socket_status to be not available

    # handle delay, add batch commands keeps adding commands
    def add_batch_cmd(self):
        print("add batch commands")
        try:
            while True:
                self.batch_cmd += input()
        except:
            pass

    # handle 'delay', execute batch commands execute each command line received during delay
    def execute_batch_cmd(self):
        print("execute batch commands")
        if self.batch_cmd:
            for cmd in self.batch_cmd.splitlines():
                self.parse_command(cmd)
        self.batch_cmd = ''

    #  execute user commands from std-input
    def parse_command(self, cmd):
        """
        :param cmd: user input command.
        :return: a signal with value 0 or 1.
                "1" represents the requested operation succeed. "O" means the connected server crash.
        """

        # read socket status
        with self.socket_status.get_lock():
            socket_status = self.socket_status.value

        if socket_status == 1:  # if socket is still connected
            with self.server_id.get_lock():
                server_id = self.server_id.value
            cmd_args = cmd.split()

            # put command
            if cmd_args[0] == "put" and len(cmd_args) == 3:
                message = str(self.client_id) + " " + cmd
                self.channel.unicast_tcp(server_id, message)

            # get command
            elif cmd_args[0] == "get" and len(cmd_args) == 2:
                message = str(self.client_id) + " " + cmd
                self.channel.unicast_tcp(server_id, message)

            # delay command: allow a batch of commands
            elif cmd_args[0] == "delay" and len(cmd_args) == 2:
                sleep_time = float(cmd_args[1])
                t_batch_cmd = multiprocessing.Process(target = self.add_batch_cmd, args=())
                t_batch_cmd.start()

                # wait until thread timeout
                time.sleep(sleep_time / 1000.0)
                t_batch_cmd.terminate()

                print("timeout")
                self.execute_batch_cmd()

            # dump command: requests server to print all variables to std out
            elif cmd_args[0] == "dump" and len(cmd_args) == 1:
                message = str(self.client_id) + " " + cmd
                self.channel.unicast_tcp(server_id, message)
            else:
                print("unable to recognize the command, enter new command:")
            return 1
        else:
            # return server crash signal to main
            return 0
開發者ID:weiyangedward,項目名稱:Key-Value-Store-Consistency,代碼行數:104,代碼來源:client.py


注:本文中的channel.Channel.unicast_tcp方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。