2017-02-20 11 views
0

以下のコードセグメントでは、実行スレッド数を20スレッドに制限したいと思います。一度カウンタが20に達すると、新しいスレッドは作成されませんが、 "a"の値はdo_something()関数をトリガしません(これは配列のすべての "a"を考慮する必要があるためです) )。どんな助けでも大歓迎です。Pythonスレッド数の制限を許可

count = 0 
for i in range(len(array_of_letters)): 

    if i == "a": 
     if count < 20: 
      count=+1 
      t = threading.Thread(target=do_something, args = (q,u)) 

      print "new thread started : %s"%(str(threading.current_thread().ident))  
      t.start() 
      count=-1 
+1

あなたはおそらく 'count = 1 +'(これはちょうど 'count = 1'、' -'と同じです。 –

答えて

1

concurrent.futures多くのタスクを提出することができますし、作業スレッドの最大数を指定ThreadPoolExecutorクラスを、持っている:

with ThreadPoolExecutor(max_workers=20) as executor: 
    for letter in array_of_letters): 
     executor.submit(do_something, letter) 

は、パッケージのドキュメントでより多くの例を確認してください。

関連する問題