import sys import os, time import os.path global folder import threading global it #from gi.repository import Gtk, GObject global Thread1 global process import numpy global num_batches if len(sys.argv) < 3: print "Invalid arguments" print "Usage : python server_script_threads.py " print "Example: python server_script_threads.py 3" sys.exit() instance_id = int(sys.argv[1]) num_instances = int(sys.argv[2]) print 'Starting instance id: %d, num_batches:%d'%(instance_id, num_instances) class servercheck(): i=0 path=("/home/ubuntu/") print(path) path_to_watch =os.path.join("/home/ubuntu/") before = dict ([(f, None) for f in os.listdir (path_to_watch)]) dir=os.chdir(os.path.join("/home/ubuntu/")) it=time.time() while 1: print "Checking for zip file..." time.sleep (1) after = dict ([(f, None) for f in os.listdir (path_to_watch)]) added = [f for f in after if not f in before] removed = [f for f in before if not f in after] #print(removed) if removed: print "removed: ", ", ".join (removed) if added: added_file=", ".join (added) print "added: ", ", ".join (added) i=i+1 if i<4: file_name=("celldata.7z") added_file=", ".join (added) if added_file == file_name: time.sleep(130) print "Removing celldata folder" os.system("rm -rf celldata") print "Unzipping celldata.7z" os.system("7z x celldata.7z") print "Removing zip file - celldata.7z" os.system("rm celldata.7z") if os.path.isdir("./images"): os.system("cp -r celldata/* images/") else: os.mkdir("./images/") os.system("cp -r celldata/* images/") global ProcessThreads ProcessThreads = [] print os.getcwd() + '/celldata' numImages = len(os.listdir(os.getcwd() + '/celldata')) print "Number of images in the folder: %d"%numImages numThreads = 5 numImagesPerThread = numImages/numThreads print "Starting %d threads- Total images: %d, Images per thread:%d"%(numThreads, numImages, numImagesPerThread) image_num_min = 2 # Starting from image 2 as image 1 is always black! 39,5 28% image_num_max = 0 for i in range (1, numThreads+1): image_num_max = image_num_min + numImagesPerThread if image_num_max > 10000: image_num_max=10000 t = threading.Thread(target=process,args=(i, image_num_min, image_num_max)) t.start() ProcessThreads.append(t) print 'Started thread %d for images %d to %d'%(i, image_num_min, image_num_max) image_num_min = image_num_max + 1 for t in ProcessThreads: t.join() totalCount = 0 for i in range (1, numThreads+1): result_file = 'count' + str(i) + '.txt' f = open(result_file, 'r') count_str = f.readline() count = int(count_str.split("=")[1]) totalCount = totalCount + count print "Thread:%d, Count:%d, Total Count:%d"%(i, count, totalCount) os.system("rm -f " + result_file) print totalCount print totalCount writefile(totalCount) #checkcountfiles() before=after def process(threadId, frame_no_min, frame_no_max): result_file = 'count' + str(threadId) + '.txt' cmd = 'sudo ./run_malariaExecutableArgs.sh /usr/local/MATLAB/MATLAB_Compiler_Runtime/v83/ %d %d %s'%(frame_no_min, frame_no_max, result_file) print "ThreadId: %d, cmd =%s"%(threadId, cmd) os.system(cmd) return def writefile(count): #testsite_array = [] #with open('PatientInformation.txt','r') as my_file: # testsite_array = my_file.readlines() #count=numpy.sum(testsite_array) file_name = 'Instance' + str(instance_id) + '.txt' f = open(file_name,'w') f.write('%d'%count) f.flush() f.close() cmd = 'sudo scp -i "jayesh1.pem" ' + file_name + ' ubuntu@ec2-54-200-214-228.us-west-2.compute.amazonaws.com:' print cmd os.system(cmd) def checkcountfiles(): path = '/home/ubuntu/' files = [] countFiles=1 iteration=0 totalCount=0 cellcount=0 textf1='Instance'+str(countFiles)+'.txt' textf2='Instance'+str(countFiles+1)+'.txt' textf3='Instance'+str(countFiles+2)+'.txt' while True: print "Checking for count files from Instances" time.sleep(1) if os.path.isfile(textf1) and os.path.isfile(textf2) and os.path.isfile(textf3): #print i iteration=iteration+1 print countFiles textf='Instance'+str(iteration)+'.txt' f = open(textf,'r') count_str = f.readline() cellcount = int(count_str) print 'The count of instance %d =%d'%(iteration,cellcount) totalCount = totalCount + cellcount if iteration == num_instances: print 'condition met' print 'total count =%d'%totalCount file_name="totalCount.txt" f = open(file_name,'w') f.write('%d'%totalCount) f.flush() f.close() return else: print "Count files have not arrived" 85%