У меня возникли проблемы с использованием модуля threading и scipy.stats.randint. Действительно, когда запускается несколько потоков, локальный массив (bootIndexs в приведенном ниже коде), похоже, используется для всех запущенных потоков.
Это повышенная ошибка
> 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) )
Спасибо за ваши ответы.