簡體   English   中英

嘗試調用Python函數時,我調用的主要位置是重啟

[英]When try to call a Python function the main where I call restart

我的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,並從另一個類中獲取所有滑動窗口。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM