4
以下はPython 3のコードです。常に100000を取得できます。なぜ間違っているのですか?私はそれが異なる結果を持っているべきだと思います。Python 2/3のスレッドで異なる結果
import time, _thread
global count
count = 0
def test():
global count
for i in range(0, 10000):
count += 1
for i in range(0, 10):
_thread.start_new_thread(test,())
time.sleep(5)
print(count)
以下はPython 2のコードです。これは常に異なる結果(ランダム)です。
import time, thread
global count
count = 0
def test():
global count
for i in range(0, 10000):
count += 1
for i in range(0, 10):
thread.start_new_thread(test,())
time.sleep(5)
print count
Pythonの3がはるかに高速ループを反復処理である、(1) 'と'私の範囲では(10000) ' –
は私の答えを参照してください'睡眠を設定してみてくださいPython 3のほうが高速です。スレッドを変更するための_when_の条件が変更されました。 –