Многопроцессорная обработка Python atexit Ошибка “Ошибка в atexit. _ run_exitfuncs”

Рассмотрим этот цикл

ifstream myFile;
myFile.open("lists.txt");
// the loop is supposed to read each line ofrom the file
// I'll assume that 'line' is a std::string
while (getline(myFile, line ))
{
    istringstream ss(line);
    while (ss)
    {
        // What type is 'a'? 
        ss >> a;
        list1.addNode(a);

        // Even assuming that 'a' is a char, std::getline extract the newline fromn the input,
        // but it doesn't append it to the string. So there is no '\n' in 'ss'
        if (a == '\n')
            break;

        // Now, even if the last element in the row has been extracted, 'ss' is still good
        // It's only the next attempt of 'ss >> a' which fails
    }

    // The following will never execute, because either the line has been consumed
    // entirely in the previous loop, or some previous read has failed
    while (ss)
    {
        ss >> a;
        list2.addNode(a);
        if (a == '\n')
        break;
    }

    // The loop isn't really a loop, it breaks unconditionally
    break;  
}

. Вы должны проверить потоки после попытки извлечения.

while (getline(myFile, line ))
{
    istringstream s1(line);
    while (s1 >> a)
    {
        list1.addNode(a);
    }

    if ( !getline(myFile, line ) )
         break;

    istringstream s2(line);
    while (s2 >> a)
    {
        list2.addNode(a);
    }
}
9
задан 19 May 2009 в 15:13
поделиться

1 ответ

Вместо того, чтобы просто принудительно использовать sys.exit () , вы хотите отправить сигнал своим потокам, чтобы они остановились. Изучите возможность использования обработчиков сигналов и потоков в Python.

Вы можете потенциально сделать это, изменив свой while True: цикл на while keep_processing: where keep_processing - это своего рода глобальная переменная, которая устанавливается в исключении KeyboardInterrupt. Я не думаю, что это хорошая практика.

1
ответ дан 5 December 2019 в 02:28
поделиться
Другие вопросы по тегам:

Похожие вопросы: