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


Python Channel.recv方法代码示例

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


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

示例1: Process

# 需要导入模块: from channel import Channel [as 别名]
# 或者: from channel.Channel import recv [as 别名]
class Process(multiprocessing.Process):
    """
        This process could take arguments from command line and execute unicast/multicast actions.
        It can also receive messages from its channel.
    """

    def __init__(self, id, ordering):
        super(Process, self).__init__() # call __init__ from multiprocessing.Process
        self.id = id

        # Read config from file
        self.process_info, self.addr_dict = configreader.get_processes_info()
        address = self.process_info[id]
        ip, port = address[0], address[1]
        print(ip, port)

        # Init a socket
        self.socket = socket.socket(
            socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.bind(address)

        # Init channel

        # Causal Order Channel
        if ordering == 'causal':
            print 'causal ordering'
            self.channel = CausalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict)
        elif ordering == 'total':
            print 'total ordering'
            # Total Order Channel
            if id == 1:
                # Select process 1 to be the sequencer
                self.channel = TotalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict, True)
            else:
                self.channel = TotalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict)
        else:
            # Regular channel
            print 'no ordering'
            self.channel = Channel(self, self.id, self.socket, self.process_info, self.addr_dict)

    def run(self):
        while True:
            data, address = self.socket.recvfrom(4096)
            self.channel.recv(data, address)

    def unicast_receive(self, source, message):
        print (message.receive_str())

    def unicast(self, destination, message):
        self.channel.unicast(message, destination)

    def multicast(self, message):
        self.channel.multicast(message)
开发者ID:weiyangedward,项目名称:cs425_MP,代码行数:55,代码来源:process.py

示例2: main

# 需要导入模块: from channel import Channel [as 别名]
# 或者: from channel.Channel import recv [as 别名]
def main():
    p = subprocess.Popen(["python", "child.py"],
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE)
    ch = Channel(p.stdin, p.stdout)
    ch.send("Hello, world!")
    print ch.recv()
    ch.send([1, 2, 3])
    print ch.recv()
    ch.send({"host": "python.org", "port": 80})
    print ch.recv()
    ch.send("stop")
    p.wait()
开发者ID:vhnuuh,项目名称:pyutil,代码行数:15,代码来源:parent.py

示例3: Client

# 需要导入模块: from channel import Channel [as 别名]
# 或者: from channel.Channel import recv [as 别名]
class Client(multiprocessing.Process):
    """
        a client sends requests to a server
    """

    def __init__(self, client_id, server_id):
        super(Client, self).__init__() # call __init__ from multiprocessing.Process

        # a shared variable to show serverID
        self.server_id = multiprocessing.Value('i', 0)
        with self.server_id.get_lock():
            self.server_id.value = server_id + 1 # (1,10)

        # a shared variable to show socket status
        self.socket_status = multiprocessing.Value('i', 0)
        with self.socket_status.get_lock():
            self.socket_status.value = 0

        # read process config from file
        self.process_info, self.addr_dict = configreader.get_processes_info()
        self.total_server = configreader.get_total_servers()
        self.batch_cmd = ''
        self.reconnect_try = 0
        self.client_id = client_id

        # init a TCP socket to connect to server
        with self.server_id.get_lock():
            self.address = self.process_info[self.server_id.value]
        self.ip, self.port = self.address[0], self.address[1]

        # robust way to find an available port
        for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                self.socket = socket.socket(af, socktype, proto) # init TCP socket
                with self.socket_status.get_lock():
                    self.socket_status.value = 1
            except socket.error as msg:
                self.socket = None
                with self.socket_status.get_lock():
                    self.socket_status.value = 0
                continue
            try:
                self.socket.connect(sa) # connect to TCP socket
            except socket.error as msg:
                self.socket.close()
                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.
#.........这里部分代码省略.........
开发者ID:weiyangedward,项目名称:Key-Value-Store-Consistency,代码行数:103,代码来源:client.py

示例4: Client

# 需要导入模块: from channel import Channel [as 别名]
# 或者: from channel.Channel import recv [as 别名]
class Client(multiprocessing.Process):
    """
        a client sends requests to a server
    """

    def __init__(self, client_id, serverID):
        super(Client, self).__init__() # call __init__ from multiprocessing.Process

        self.server_id = multiprocessing.Value('i', 0)
        with self.server_id.get_lock():
            self.server_id.value = serverID + 1 # (1,10)

        self.socket_status = multiprocessing.Value('i', 0)
        with self.socket_status.get_lock():
            self.socket_status.value = 0

        """
            Read config from file
            process_info[id] = (ip, port)
            addr_dict[(ip, port)] = id
        """
        self.process_info, self.addr_dict = configreader.get_processes_info()
        self.total_server = configreader.get_total_servers()
        self.batch_cmd = ''
        self.reconnect_try = 0
        self.client_id = client_id
        # self.exit = sys.exit()

        """
            init TCP connect to server
        """
        with self.server_id.get_lock():
            self.address = self.process_info[self.server_id.value]
        self.ip, self.port = self.address[0], self.address[1]
        # print(self.ip, self.port)
        for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                self.socket = socket.socket(af, socktype, proto) # build a socket
                with self.socket_status.get_lock():
                    self.socket_status.value = 1
            except socket.error as msg:
                self.socket = None
                with self.socket_status.get_lock():
                    self.socket_status.value = 0
                continue
            try:
                self.socket.connect(sa) # connect to socket 
            except socket.error as msg:
                self.socket.close()
                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 unicast channel
        """
        print("client, no ordering")
        # pass Client obj as arg to Channel
        self.channel = Channel(self, self.client_id, self.socket, self.process_info, self.addr_dict)

    """
        override run(), receiving message from server
    """
    def run(self):
        try:

            while True:
                data, address = self.socket.recvfrom(4096)
                print("get server message: ", data, address)
                # break if connected server crashed
                if not data: break
                self.channel.recv(data)
        except:
            print("run() Exceptions")
        finally:
            print("exit client thread")
            if self.socket != None: 
                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'
        addBatchCmd keeps adding commands
    """
    def addBatchCmd(self):
        print("addBatchCmd...")
        try:
            while True:
                self.batch_cmd += input()
        except:
            pass
#.........这里部分代码省略.........
开发者ID:weiyangedward,项目名称:cs425_MP,代码行数:103,代码来源:client.py

示例5: Channel

# 需要导入模块: from channel import Channel [as 别名]
# 或者: from channel.Channel import recv [as 别名]
# encoding:utf-8
from channel import Channel
import sys


ch = Channel(sys.stdout, sys.stdin)
while True:
    item = ch.recv()
    if item == "stop":
        break
    else:
        ch.send(("child", item))
开发者ID:vhnuuh,项目名称:pyutil,代码行数:14,代码来源:child.py

示例6: Client

# 需要导入模块: from channel import Channel [as 别名]
# 或者: from channel.Channel import recv [as 别名]
class Client(multiprocessing.Process):
    """
        a client sends requests to a server
    """

    def __init__(self, client_id, serverID):
        super(Client, self).__init__() # call __init__ from multiprocessing.Process
        self.serverID = serverID - 1 # (0,9)
        self.client_id = client_id

        """
            Read config from file
            process_info[id] = (ip, port)
            addr_dict[(ip, port)] = id
        """
        self.process_info, self.addr_dict = configreader.get_processes_info()
        self.total_server = configreader.get_total_servers()
        self.address = self.process_info[self.serverID+1]
        self.ip, self.port = self.address[0], self.address[1]
        self.batch_cmd = ''
        self.client_id = client_id
        self.reconnect_try = 0

        """
            init TCP connect to server
        """
        self.address = self.process_info[self.serverID+1]
        self.ip, self.port = self.address[0], self.address[1]
        for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                self.socket = socket.socket(af, socktype, proto) # build a socket
            except socket.error as msg:
                self.socket = None
                continue
            try:
                self.socket.connect(sa) # connect to socket 
            except socket.error as msg:
                self.socket.close()
                self.socket = None
                continue
            break
        if self.socket is None:
            print('could not open socket')
            sys.exit(1)

        """
            Init unicast channel
        """
        print 'client, no ordering'
        # pass Client obj as arg to Channel
        self.channel = Channel(self, self.client_id, self.socket, self.process_info, self.addr_dict)

    def socketTCP(self):
        self.address = self.process_info[self.serverID+1]
        self.ip, self.port = self.address[0], self.address[1]
        for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                self.socket = socket.socket(af, socktype, proto) # build a socket
            except socket.error as msg:
                self.socket = None
                continue
            try:
                self.socket.connect(sa) # connect to socket 
            except socket.error as msg:
                self.socket.close()
                self.socket = None
                continue
            break
        if self.socket is None:
            print('could not open socket')
        else:
            print("successfully connected to server %d" % (self.serverID+1))

    def channelSet(self):
        self.channel = Channel(self, self.client_id, self.socket, self.process_info, self.addr_dict)
            # sys.exit(1)

    """
        override run(), receiving message from server
    """
    def run(self):
        try:
            while True:
                data, address = self.socket.recvfrom(4096)
                print("get server message: ", data, address)
                # break if connected server crashed
                if not data: break
                self.channel.recv(data)
        except KeyboardInterrupt:
            print("CTRL C occured")
        except:
            print("Disconnected from server")
            # ====
            # choose another server if the connected server is crashed
            # handle server crash: pick a server with higher id
            # relaunch run()
            # ====
            
#.........这里部分代码省略.........
开发者ID:weiyangedward,项目名称:cs425_MP,代码行数:103,代码来源:client.py


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