#!/usr/bin/python

import threading
import time
import pdb
import os
import pycassa
import sys


pool = pycassa.ConnectionPool('Blast', server_list=['1.1.1.1','2.2.2.2','3.3.3.3','4.4.4.4','5.5.5.5','6.6.6.6','7.7.7.7','8.8.8.8','9.9.9.9','10.10.10.10','11.11.11.11','12.12.12.12'],pool_size=32,timeout=60)
cf = pycassa.ColumnFamily(pool, 'Blast_NR')
inp_file=open(sys.argv[1])
lines=inp_file.readlines()
inp_file.close()

class myThread (threading.Thread):
    def __init__(self, threadID, tname):
        self.threadID = threadID
        self.tname = tname
        threading.Thread.__init__(self)
    def run(self):
        print "Starting " + self.name
        st=time.time()
        start_cassandra_client(self.name)
        et=time.time()
        print self.name + " - Time for querying all keys is" + str(round(et-st,2)) + " secs"

		
def start_cassandra_client(Threadname):
        f=open(Threadname,"w")
        for key in lines:
                key=key.strip()
                st=time.time()
                f.write(str(cf.get(key))+"\n")
                et=time.time()
                f.write("Time taken for a single query is " + str(round(1000*(et-st),2))+" milli secs\n")
        f.close()

threads = []
for i in range(4):
	# Create new threads
	thread = myThread(i, "Thread-"+str(i))
	thread.start()
	threads.append(thread)

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