Using multi threads in python

Post date: Sep 10, 2016 2:07:21 PM

This post shows how to use threading, a popular thread package in python, to run multiple queries in parallel. This example also illustrates using threading with methods inside class in wo ways: 1) function inside object Obj().func(x,y) and 2) function run on the object Obj(x, y).func.

from redshift.execute_redshift_query import execute_redshift_query import threading import time class myThread (threading.Thread): def __init__(self, id, name, func_name, func_params): threading.Thread.__init__(self) self.id = id self.name = name self.func_name = func_name self.func_params = func_params def run(self): print "Starting " + self.name self.func_name(**self.func_params) print "Exiting " + self.name class MyObject(): def get_estimate_historical(self, pid, day_a, day_b): query = \ ''' select * from estimate_historical where property_id = {pid} and day >= '{day_a}' and day <= '{day_b}' '''.format(pid=pid, day_a=day_a, day_b=day_b) data, columns = execute_redshift_query(query, named_columns=True) print data class MyObject2(): def __init__(self, pid, day_a, day_b): self.pid = pid self.day_a = day_a self.day_b = day_b def get_estimate_historical(self): query = \ ''' select * from estimate_historical where property_id = {pid} and day >= '{day_a}' and day <= '{day_b}' '''.format(pid=self.pid, day_a=self.day_a, day_b=self.day_b) data, columns = execute_redshift_query(query, named_columns=True) print data if __name__ == '__main__': # using multithread for functions in Class t1 = myThread(1, 'id1', MyObject().get_estimate_historical, {'pid': 200101, 'day_a': '2016-08-01', 'day_b': '2016-09-02'}) t2 = myThread(2, 'id2', MyObject().get_estimate_historical, {'pid': 223225, 'day_a': '2016-08-01', 'day_b': '2016-09-02'}) # Another case where the function received params from object itself t3 = myThread(3, 'id3', MyObject2(211111, '2016-08-01', '2016-09-02').get_estimate_historical, {}) t1.start() print t1.isAlive() t2.start() print t2.isAlive() t3.start() print t3.isAlive() # waiting loop cnt = 0 while t1.isAlive() or t2.isAlive() or t3.isAlive(): print "I'm waiting {0}".format(cnt) time.sleep(1) cnt += 1 print t1.isAlive() print t2.isAlive() print t3.isAlive()

Further resources about threads in python

http://www.tutorialspoint.com/python/python_multithreading.htm

http://stackoverflow.com/questions/15063963/python-is-thread-still-running