Use single connection

This commit is contained in:
Roman Grundkiewicz 2018-10-19 12:04:10 +01:00
parent 410f4d05bc
commit df526c1fe3

View File

@ -9,24 +9,15 @@ import argparse
from websocket import create_connection
def translate(batch, port=8080):
ws = create_connection("ws://localhost:{}/translate".format(port))
#print(batch.rstrip())
ws.send(batch)
result = ws.recv()
print(result.rstrip())
ws.close()
def parse_args():
if __name__ == "__main__":
# handle command-line options
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--batch-size", type=int, default=1)
parser.add_argument("-p", "--port", type=int, default=8080)
return parser.parse_args()
args = parser.parse_args()
if __name__ == "__main__":
args = parse_args()
# open connection
ws = create_connection("ws://localhost:{}/translate".format(args.port))
count = 0
batch = ""
@ -34,9 +25,19 @@ if __name__ == "__main__":
count += 1
batch += line.decode('utf-8') if sys.version_info < (3, 0) else line
if count == args.batch_size:
translate(batch, port=args.port)
# translate the batch
ws.send(batch)
result = ws.recv()
print(result.rstrip())
count = 0
batch = ""
if count:
translate(batch, port=args.port)
# translate the remaining sentences
ws.send(batch)
result = ws.recv()
print(result.rstrip())
# close connection
ws.close()