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 に答える