dynamically instantiate classes in Python
I want to dynamically add threading.Thread classes to a thread queue based on a database query. Is that possible?
import threading, Queue class worker(threading.Thread): def run(self): while True: print 'doing stuff' # get jobs from db jobs = list(db.Jobs.find()) q = Queue.Queue(5) for job in jobs: # instantiate a worker thread here.. don't know how to do this ... # start new worker thread new_worker_thread.start() # then add the worker to the queue q.put(new_worker_thread)
Any advice would be awesome.
new_worker_thread = worker()
This instantiates a new worker thread. After this you can start and put it in the queue.
Some more information on threads can be found here: http://www.tutorialspoint.com/python/python_multithreading.htm
Very useful tutorial!