私は複数のスレッドがあります。上記のコードは私にいくつかの非常に奇妙な行動を与えていたthreading.Lock()パフォーマンス上の問題
dispQ = Queue.Queue()
stop_thr_event = threading.Event()
def worker (stop_event):
while not stop_event.wait(0):
try:
job = dispQ.get(timeout=1)
job.waitcount -= 1
dispQ.task_done()
except Queue.Empty, msg:
continue
# create job objects and put into dispQ here
for j in range(NUM_OF_JOBS):
j = Job()
dispQ.put(j)
# NUM_OF_THREADS could be 10-20 ish
running_threads = []
for t in range(NUM_OF_THREADS):
t1 = threading.Thread(target=worker, args=(stop_thr_event,))
t1.daemon = True
t1.start()
running_threads.append(t1)
stop_thr_event.set()
for t in running_threads:
t.join()
。 私はそれが私が「
が、私はこれが思わ
with job.thr_lock:
job.waitcount -= 1
にそれを変更しましたself.thr_lock = threading.Lock()
、ジョブクラスに属性を追加しましロックアウトでwaitcountを減少によるものであったことを見つける終わってきました奇妙な動作を修正するが、性能が低下しているように見える。
これは予想されますか?ロックを最適化する方法はありますか?
ジョブオブジェクトごとに1つのロックではなく、1つのグローバルロックを持つ方がよいでしょうか?
'job.waitcount'の設定または増分は何ですか? – martineau
ところで、あなたは 'except Queue.Empty as msg:'を使うべきです。カンマで書かれた構文はpython2でのみ動作し、 'as'はpython2.6、2.7、3+で動作します。また、複数のタイプの例外をキャッチしたい場合は、他の言語と一貫しています。 – Bakuriu