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)
어떤 조언 좋지 않을까.
[PEP-8] (http://www.python.org/dev/peps/pep-0008/) (파이썬 스타일 가이드)는'CapWords'를 클래스 용으로 예약하고'lowercase_with_underscores '변수에 대해. 코드를보다 읽기 쉽게 만들려면이 지침을 따르는 것이 좋습니다. –
Ahm,'new_worker_thread = worker()'의 무엇이 문제입니까? –
아, 같은 이름의 작업자를 인스턴스화해도 기존 개체를 덮어 쓰지 않기 때문에 실현되었습니다. – MFB