Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 线程错误:can';不要开始新的线程_Python_Multithreading_Numpy_Montecarlo_Kernel Density - Fatal编程技术网

Python 线程错误:can';不要开始新的线程

Python 线程错误:can';不要开始新的线程,python,multithreading,numpy,montecarlo,kernel-density,Python,Multithreading,Numpy,Montecarlo,Kernel Density,这里是我正在使用的一个更大的代码的MWE。它在KDE()上对位于某个阈值以下的所有值(在这个问题上建议使用积分方法:)执行蒙特卡罗积分,迭代地对列表中的多个点执行积分,并返回由这些结果组成的列表 import numpy as np from scipy import stats from multiprocessing import Pool import threading # Define KDE integration function. def kde_integration(m_l

这里是我正在使用的一个更大的代码的
MWE
。它在KDE()上对位于某个阈值以下的所有值(在这个问题上建议使用积分方法:)执行蒙特卡罗积分,迭代地对列表中的多个点执行积分,并返回由这些结果组成的列表

import numpy as np
from scipy import stats
from multiprocessing import Pool
import threading

# Define KDE integration function.
def kde_integration(m_list):

    # Put some of the values from the m_list into two new lists.
    m1, m2 = [], []
    for item in m_list:
        # x data.
        m1.append(item[0])
        # y data.
        m2.append(item[1])

    # Define limits.
    xmin, xmax = min(m1), max(m1)
    ymin, ymax = min(m2), max(m2)

    # Perform a kernel density estimate on the data:
    x, y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
    values = np.vstack([m1, m2])
    kernel = stats.gaussian_kde(values)

    # This list will be returned at the end of this function.
    out_list = []

    # Iterate through all points in the list and calculate for each the integral
    # of the KDE for the domain of points located below the value of that point
    # in the KDE.
    for point in m_list:

        # Compute the point below which to integrate.
        iso = kernel((point[0], point[1]))

        # Sample KDE distribution
        sample = kernel.resample(size=1000)

        #Choose number of cores and split input array.
        cores = 4
        torun = np.array_split(sample, cores, axis=1)

        # Print number of active threads.
        print threading.active_count()

        #Calculate
        pool = Pool(processes=cores)
        results = pool.map(kernel, torun)

        #Reintegrate and calculate results
        insample_mp = np.concatenate(results) < iso

        # Integrate for all values below iso.
        integral = insample_mp.sum() / float(insample_mp.shape[0])

        # Append integral value for this point to list that will return.
        out_list.append(integral)

    return out_list


# Generate some random two-dimensional data:
def measure(n):
    "Measurement model, return two coupled measurements."
    m1 = np.random.normal(size=n)
    m2 = np.random.normal(scale=0.5, size=n)
    return m1+m2, m1-m2

# Create list to pass to KDE integral function.
m_list = []
for i in range(100):
    m1, m2 = measure(5)
    m_list.append(m1.tolist())
    m_list.append(m2.tolist())

# Call KDE integration function.
print 'Integral result: ', kde_integration(m_list)
通常(10次中的9次)围绕活动线程
374
。在这里,我在
python
编码方面远远超出了我的能力范围,我不知道如何解决这个问题。任何帮助都将不胜感激


添加 我尝试添加
while
循环,以防止代码使用过多线程。我所做的是将
打印线程.active\u count()
行替换为以下代码位:

    # Print number of active threads.
    exit_loop = True
    while exit_loop:
        if threading.active_count() < 300:
            exit_loop = False
        else:
            # Pause for 10 seconds.
            time.sleep(10.)
            print 'waiting: ', threading.active_count()
#打印活动线程数。
退出循环=真
退出_循环时:
如果线程.active_count()小于300:
退出循环=错误
其他:
#暂停10秒钟。
时间。睡眠(10)
打印'waiting:',threading.active_count()
代码到达
302
活动线程时停止(即:卡在循环内)。我等待了10多分钟,代码从未退出循环,活动线程的数量从未从
302
中下降。活动线程的数量不是应该在一段时间后减少吗

    # Print number of active threads.
    exit_loop = True
    while exit_loop:
        if threading.active_count() < 300:
            exit_loop = False
        else:
            # Pause for 10 seconds.
            time.sleep(10.)
            print 'waiting: ', threading.active_count()