import threading
import time
import subprocess

file_symbol=open("symbols.txt","r")
symbols=file_symbol.read().splitlines()
#print(symbols)
symbols = [x.strip(' ') for x in symbols]

#symbols=symbols[:-5]
class myThread (threading.Thread):
   def __init__(self, threadID):
      threading.Thread.__init__(self)
      self.threadID = threadID
      #self.name = name
      #self.counter = counter
   def run(self):
      print("Starting Thread Number %s"%self.threadID)
      # Get lock to synchronize threads
      #threadLock.acquire()
      #self.status="idle"
      while len(symbols)!=0:
         #self.status="busy"
         self.symbol=symbols.pop(0)
         self.job=subprocess.call(['gnome-terminal','-x','taskset','-c',str(self.threadID),'python3','predictor.py',self.symbol,"800"])
         time.sleep(99999)
         print("Prediction for %s done"%self.symbol)
      print("Job for Thread Number %s done"%self.threadID)
      #print_time(self.name, self.counter, 3)
      # Free lock to release next thread
      #threadLock.release()

def print_time(threadName, delay, counter):
   while counter:
      time.sleep(delay)
      print("%s: %s" % (threadName, time.ctime(time.time())))
      counter -= 1

#threadLock = threading.Lock()
threads = []


for numberID in range(0,len(symbols)-1,1):
   thread = myThread(numberID)
   thread.start()
   threads.append(thread)
   #threadID += 1
# Create new threads
#thread1 = myThread(1, "Thread-1", 1)
#thread2 = myThread(2, "Thread-2", 2)

# Start new Threads
#thread1.start()
#thread2.start()

# Add threads to thread list
#threads.append(thread1)
#threads.append(thread2)

# Wait for all threads to complete
for t in threads:
    t.join()
print("Exiting Main Thread")
