threadingとscipy.stats.randintモジュールを使用する際に問題があります。実際、いくつかのスレッドが起動されると、起動されたすべてのスレッドでローカル配列(以下のコードのbootIndexs)が使用されているようです。Pythonでスレッディングする:ローカル変数に問題がある
これは、発生したエラー
> Exception in thread Thread-559:
Traceback (most recent call last):
...
File "..\calculDomaine3.py", line 223, in bootThread
result = bootstrap(nbB, distMod)
File "...\calculDomaine3.py", line 207, in bootstrap
bootIndexs = spstats.randint.rvs(0, nbTirages-1, size = nbTirages)
File "C:\Python27\lib\site-packages\scipy\stats\distributions.py", line 5014, in rvs
return super(rv_discrete, self).rvs(*args, **kwargs)
File "C:\Python27\lib\site-packages\scipy\stats\distributions.py", line 582, in rvs
vals = reshape(vals, size)
File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 171, in reshape
return reshape(newshape, order=order)
ValueError: total size of new array must be unchanged
であり、これは私のコードです:あなたの答えのための
import threading
import Queue
from scipy import stats as spstats
nbThreads = 4
def test(nbBoots, nbTirages, modules):
def bootstrap(nbBootsThread, distribModules) :
distribMax = []
for j in range(nbBootsThread):
bootIndexs = spstats.randint.rvs(0, nbTirages-1, size = nbTirages)
boot = [distribModules[i] for i in bootIndexs]
distribMax.append(max(boot))
return distribMax
q = Queue.Queue()
def bootThread (nbB, distMod):
result = bootstrap(nbB, distMod)
q.put(result, False)
q.task_done()
works = []
for i in range(nbThreads) :
works.append(threading.Thread(target = bootThread, args = (nbBoots//nbThreads, modules[:],)))
for w in works:
w.daemon = True
w.start()
q.join()
distMaxResult = []
for j in range(q.qsize()):
distMaxResult += q.get()
return distMaxResult
class classTest:
def __init__(self):
self.launch()
def launch(self):
print test(100, 1000, range(1000))
感謝。
あなたのコードでは、動作を説明するものは何も表示されません。実行して実験することができる自己完結型の例を考え出すことができますか? – NPE
私はそれを編集します。scipyを持っているなら、このように使うことができます – user1062526
(nbThreadsを 'nbThreads'に変更してしまえば)コードを実行でき、別のエラーが出ます(' Thread- 4(通訳者のシャットダウン中に発生する可能性が高い) ')。 – NPE