Commit a222b9a8 authored by Suzhi Bi's avatar Suzhi Bi

Upload New File

parent 612b9901
import subprocess
import socket
import sys
import os
import struct
import time
def deal_data(conn, addr):
print ('Accept new connection from {0}'.format(addr))
fileinfo_size = struct.calcsize('128sq')
buf = conn.recv(fileinfo_size)
global flag
flag = 0
if buf:
if buf.decode('utf-8', 'ignore').isdigit() and len(buf.decode('utf-8', 'ignore')) < 5:
global time_max
time_max = int(buf.decode('utf-8'))
print('The maximum time (seconds) allowed for picture(s) is %d.' %time_max)
flag = 1
else:
global filesize
filename, filesize = struct.unpack('128sq', buf)
fn = filename.strip(str.encode('\00'))
global new_filename
new_filename = os.path.join(str.encode('./'), fn)
print ('file new name is {0}, filesize if {1}'.format(new_filename, filesize))
recvd_size = 0
fp = open(new_filename, 'wb')
print ("start receiving...")
while not recvd_size == filesize:
if filesize - recvd_size > 1024:
data = conn.recv(1024)
recvd_size += len(data)
else:
data = conn.recv(filesize - recvd_size)
recvd_size = filesize
fp.write(data)
fp.close()
print("end receive...")
else:
flag = 1
print("not received...")
conn.close()
ip = '192.168.191.4'
print ("Waiting...")
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((ip, 8080))
s.listen(10)
except socket.error as msg:
print(msg)
sys.exit(1)
data_size = []
name = []
result = []
image_location = []
python_code = []
new_image = []
k = -1
while 1:
conn, addr = s.accept()
deal_data(conn, addr)
if flag == 1:
continue
k = k + 1
os.system("echo $(docker ps -aq) >> 1.txt")
os.system("echo $(wc 1.txt) >> 2.txt")
f = open("2.txt", "r")
for line in f.readlines():
line = line.strip()
num = int(line.split()[1])
os.system("rm -f 1.txt 2.txt")
queueing_time = 0
while num > 30:
time.sleep(1)
os.system("echo $(docker ps -aq) >> 1.txt")
os.system("echo $(wc 1.txt) >> 2.txt")
f = open("2.txt", "r")
for line in f.readlines():
line = line.strip()
num = int(line.split()[1])
os.system("rm -f 1.txt 2.txt")
queueing_time = queueing_time + 1
new_image.append(str(new_filename, encoding="utf-8"))
data_size.append(os.path.getsize(new_image[k]))
python_code.append("/classifier_api/image_classification/classifier.py")
image_location.append("/classifier_api/" + str(k) + ".jpg")
name.append("class_" + str(k))
result.append("result_" + str(addr[0]) + "_" + str(os.path.split(new_image[k])[-1]).split('.')[0])
if data_size[k] != filesize:
message = "Error receiving " + str(os.path.split(new_image[k])[-1])
print(message)
cmd1 = "echo " + str(message) + " >> " + str(result[k]) + ".txt"
os.system(cmd1)
cmd2 = "python3 result_return.py " + str(addr[0]) + " " + str(os.path.split(new_image[k])[-1]).split('.')[0] + ".txt"
os.system(cmd2)
cmd3 = "rm -f result_" + str(addr[0]) + "_" + str(os.path.split(new_image[k])[-1]).split('.')[0] + ".txt"
os.system(cmd3)
# os.system("rm -f " + str(new_image[k]))
os.system("echo Error receiving. >> time.txt")
continue
print("start container...")
cmd1 = "docker run --name " + str(name[k]) + " -dt class_api/v1 /bin/bash"
cmd2 = "docker cp " + str(new_image[k]) + " " + str(name[k]) + ":/" + str(image_location[k])
cmd3 = "docker exec " + str(name[k]) + " python3 -W ignore::RuntimeWarning " + str(python_code[k]) + " --image_file " + str(image_location[k]) + " --num_top_predictions 1 >> " + str(result[k]) + ".txt"
cmd4 = "docker stop " + str(name[k])
cmd4_1 = "echo $(($(date --date=$(docker inspect --format='{{.State.FinishedAt}}' " + str(name[k]) + " ) +%s) - $(date --date=$(docker inspect --format='{{.State.StartedAt}}' " + str(name[k]) + " ) +%s)))\(" + str(queueing_time) + "\) seconds/" + str(time_max) + " seconds >> time.txt"
cmd5 = "docker rm " + str(name[k])
cmd6 = "python3 result_return.py " + str(addr[0]) + " " + str(os.path.split(new_image[k])[-1]).split('.')[0] + ".txt"
cmd7 = "rm -f result_" + str(addr[0]) + "_" + str(os.path.split(new_image[k])[-1]).split('.')[0] + ".txt"
# cmd8 = "rm -f " + str(new_image[k])
cmd = cmd1 + " && " + cmd2 + " && " + cmd3 + " && " + cmd4 + " && " + cmd4_1 + " && " + cmd5 + " && " + cmd6 + " && " + cmd7 # + " && " + cmd8
p = subprocess.Popen(cmd, shell = True, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment