当我尝试调用Python函数时,我调用的主要重启

时间:2017-05-22 10:55:37

标签: python multithreading

我的Python代码有问题:当我从另一个类调用该函数时,我调用该函数的类重新启动,编译器会给出此错误消息:

RuntimeError: 
    An attempt has been made to start a new process before the
    current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

我启动代码的类是:

 finestre = creatore_finestre()
    print(finestre[0])

该功能的代码是:

DIR_DATA = '../../../data/'
SIGNALS_INDEX = {
    'HR': 0,
    'ABPSys': 1,
    'ABPDias': 2,
    'ABPMean': 3,
    'CVP': 4,
    'PULSE': 5,
    'RESP': 6,
    'SpO2': 7,
    'NBPSys': 8,
    'NBPDias': 9,
    'NBPMean': 10,
}


def download_information_database(id_patient):
    wfdb.dldatabase('mimic2db/numerics', DIR_DATA + id_patient, records=[id_patient])


def create_csv(id_patient, signal):

    # Download the patient information
    download_information_database(id_patient)

    # Firstly, we read the patient information
    patient_dir = DIR_DATA + id_patient + "/"
    record = wfdb.rdsamp(patient_dir + id_patient, channels=[SIGNALS_INDEX[signal]])


    # We calculate the datetime base
    date_str = record.basedate + ' ' + record.basetime
    date = datetime.datetime.strptime(date_str, '%d/%m/%Y %H:%M:%S')

    # We read the signal values
    signal = record.p_signals

    # We write the csv file
    with open(patient_dir + id_patient + '.csv', 'w+') as csvfile:
        writer = csv.writer(csvfile, delimiter=',',lineterminator="\n")

        for s in signal:
            date = date + datetime.timedelta(minutes=1)

            if not math.isnan(float(s)):
                writer.writerow([date.strftime("'[%H:%M:%S %d/%m/%Y]'"),str(int(s[0]) * 1000)])




def creatore_finestre():

    #Open the file of information for each patients
    in_file = open("../../../data/CodePatientsTraining.csv", "r+")
    lettore_file = csv.reader(in_file)
    #Create dictionary of list
    finestre = defaultdict(list)
    for nomeFile in lettore_file:
        print(nomeFile[0])
        create_csv(nomeFile[0],"ABPMean")
        f = open("../../../data/" + nomeFile[0] + "/" + nomeFile[0] + ".csv", "r+")
        reader = csv.reader(f)
        line = in_file.readline()
        lista = list(reader)
        i = 0
        for timestamp in lista:
            if (timestamp[0] != nomeFile[1]):
                i += 1
            else:
                print(timestamp[0], nomeFile[1], i)
                break

        decade = 0
        somma = 0
        arrivo = 1
        minute = 10
        while (i != 0):
            i -= 1
            if (lista[i][1] != '0' and lista[i][1] != '-' and int(lista[i][1]) > 0):
                somma += float(lista[i][1])
                decade += 1
            if (decade == minute):
                f = SlidingWindows((somma / minute), nomeFile[4])
                finestre[arrivo].append(f)
                print("T[" + str(arrivo) + "]:")
                for value in finestre[arrivo]:
                    print(value)
                decade = 0
                arrivo += 1
                somma = 0

    return finestre

我的想法是为函数中的每个CSV文件创建一个SlidingWindows,并从另一个类中获取所有滑动窗口。

1 个答案:

答案 0 :(得分:0)

import your_module
if __name__ == '__main__':    
    extractor = your_Module.your_function()
    extractor.runInParallel(numProcesses=2, numThreads=4)