私はこのスクリプトを持っている:Pythonスレッド、なぜ2次元のスレッドが機能しないのですか?
import threading
import time
import sys
def threadWait(d1, d2):
global number
time.sleep(1) # My actions
number = number+1 # Count of complete actions +1
sys.stdout.write("\033[K") # Clean line to the end.
sys.stdout.write(str(number)+" (Thread "+str(d1)+", "+str(d2)+") done"+"\r") # Write number and carriage return.
sys.stdout.flush()
number = 0 # Count of complete actions
threadsToJoin = []
dimension1 = [] # Main action task.
for i in range(50): # I have 50 "Main Actions" I need to do in parallel threads.
d1 = i
dimension2 = [] # I need to do each "Main Action" in 10 threads.
for n in range(10):
d2 = n
dimension2.append(threading.Thread(target=threadWait, args=(d1,d2)))
dimension1.append(dimension2)
for item in dimension1:
for items in dimension2:
# But I can't do more than 100 Threads at once.
while True:
# Analogue of BoundedSemaphore.
if (int(threading.activeCount()) < 100):
items.start()
threadsToJoin.append(items)
break
else:
continue
for this in threadsToJoin:
this.join()
しかし、私は「スレッドが二回起動することはできません」というエラーを取得しています。しかし、私はdimension2
ですべてのスレッドを追加すると、次のように実行しているとき:excpectedとして
for item in dimension2:
# But I can't do more than 100 Threads at once.
while True:
# Analogue of BoundedSemaphore.
if (int(threading.activeCount()) < 100):
item.start()
break
else:
continue
Everythinが良いだろう。最初の例で何が問題になっていますか?また、スレッド化(マルチスレッド化)は今のようにどうすればできますか?
それはとてもばかげた過ちです0_O申し訳ありませんが、時には私の心をきれいにするために猫の昼寝を取る必要があります。 – passwd