Ошибка "freeze_support ()", если включено __name__ == '__main__': - PullRequest
0 голосов
/ 02 июня 2019

Я пытаюсь использовать многопроцессорный процесс, и я получаю эту ошибку 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.

но проблема в том, что __name__ == '__main__': уже есть в моем файле run.py.

Когда я гуглю эту ошибку, меня заставляют добавлять оператор if, когда он у меня уже есть.

run.py

if __name__ == "__main__":
    from inventory import update_inventory2

update_inventory2.py

print("update_inventory")
while True:
    print("starting")
    p = Process(target=add_to_queue, args=(db, ))# db is just the db being used and has no baring on my issue.
    p.start()
    p.join()

Выход:

>update_inventory
>starting
>update_inventory
>starting
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python34\lib\multiprocessing\spawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "C:\Python34\lib\multiprocessing\spawn.py", line 116, in _main
    self = pickle.load(from_parent)
  File "D:\Storage\\processes\update_inventory\update_inventory2.py", line 136, in <module>
    p.start()
  File "C:\Python34\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Python34\lib\multiprocessing\context.py", line 212, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Python34\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)
  File "C:\Python34\lib\multiprocessing\popen_spawn_win32.py", line 34, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Python34\lib\multiprocessing\spawn.py", line 144, in get_preparation_data
    _check_not_importing_main()
  File "C:\Python34\lib\multiprocessing\spawn.py", line 137, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
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.

Не следует пытаться запустить дважды, не уверен, что это связано.

...