本文整理汇总了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