1

我想根据数据库查询动态地将threading.Thread类添加到线程中。queue那可能吗?

例如:

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)

任何建议都会很棒。

4

1 回答 1

3

只需使用:

new_worker_thread = worker()

这将实例化一个新的工作线程。在此之后,您可以开始并将其放入队列中。

可以在此处找到有关线程的更多信息:http ://www.tutorialspoint.com/python/python_multithreading.htm

很实用的教程!

于 2012-06-18T00:08:17.717 回答