それは簡単concurrents.futuresライブラリ
ここでは例のコードだ使用して達成することができます:ここ
from concurrent.futures import ThreadPoolExecutor
ip = '192.168.0.'
count = 0
THREAD_COUNT = 10
def work_done(future):
result = future.result()
# work with your result here
def main():
with ThreadPoolExecutor(THREAD_COUNT) as executor:
while count <= 255:
count += 1
ipg=ip+str(count)
executor.submit(conn, ipg, 80).add_done_callback(work_done)
if __name__ == '__main__':
main()
をexecutorは、それが提出するすべてのタスクに対して未来を返します。 add_done_callback()
スレッドからの完了タスクを使用してメインスレッド(メインスレッドをブロックする)に戻った場合、真の並列処理が本当に必要な場合は、将来のオブジェクトを別々に待つ必要があることに留意してください。そのためのコードスニペットがあります。
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures._base import wait
futures = []
with ThreadPoolExecutor(THREAD_COUNT) as executor:
while count <= 255:
count += 1
ipg=ip+str(count)
futures.append(executor.submit(conn, ipg, 80))
wait(futures)
for succeded, failed in futures:
# work with your result here
これが役に立ちます。
私はキューを追加するべきだと思います –
'ThreadPool(processes = 10)で' multiprocessing.pool'の大部分の文書化されていない['ThreadPool'](http://stackoverflow.com/a/3386632/355230) ) '。 'while(使用中ではありません(結果の中でa_threadのためにすべて(a_thread.ready()を使用することができます)):pass'を実行して、各スレッドが10回すべてアイドル状態になるのを待ちます。他の質問に[私の答え](http://stackoverflow.com/a/18283388/355230)を参照してください。 – martineau
あなたの答えをありがとう、現時点で私はキューリストで作業していますが、私はプールの利点は、10の1つのプロセスが完了した後、10のすべてが完了する前に待機するのではなく、右 ? –