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?

For example:

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.

Answers


Simply use:

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!


Need Your Help

Print a deck of cards after they have been shuffled using a menu

java

My program has a menu with 3 choices. 1 is to display the deck(if the users enter 1 first a new deck will be displayed). 2 is to shuffle the deck without printing the deck. 3 just exits the program...

Auto applying 'height' data to submenu's with jQuery

javascript jquery html

I have a fairly simple menu structure, one level deep, made using UL/LI's as below...