Skip to content

Pynumero spawns multiple instances of script file from which it's called #2380

Open
@dallan-keylogic

Description

@dallan-keylogic

Summary

I ran a script file with an IDAES model. At a certain point, when IDAES was trying to retrieve the Pynumero NLP for the model, something in Pyomo or Pynumero imports or calls the script file that I was originally running, with additional calls to IPOPT and additional calls to the code that caused Pyomo/Pynumero to call the script file in the first place, resulting in more processes being spawned recursively until Pythons multiprocessing code begins killing off the child processes.

A workaround fix is to put the entire script file in a function main() and insert an if __name__ == "__main__": main() block at the end of the file, as described in this WaterTAP guide.

Error Message

The following is one of the error messages raised when a child process is killed. There are other error messages that occur in child processes, but with less extensive stack traces. Once enough child processes are killed, the original script file actually returns.

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\[REDACTED]\Work\workspace\SOC\barelli_replication.py", line 382, in <module>
    out = check_scaling(m)
  File "C:\Users\[REDACTED]\Work\workspace\SOC\barelli_replication.py", line 49, in check_scaling
    jac, nlp = iscale.get_jacobian(blk, scaled=True)
  File "C:\Users\[REDACTED]\Work\idaes-pse\idaes\core\util\scaling.py", line 619, in get_jacobian
    jac, jac_scaled, nlp = constraint_autoscale_large_jac(
  File "C:\Users\[REDACTED]\Work\idaes-pse\idaes\core\util\scaling.py", line 559, in constraint_autoscale_large_jac
    nlp = PyomoNLP(m)
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\site-packages\pyomo\contrib\pynumero\interfaces\pyomo_nlp.py", line 96, in __init__
    with CtypesEnviron(AMPLFUNC=amplfunc):
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\site-packages\pyomo\common\env.py", line 440, in __init__
    self.interfaces.extend(_RestorableEnvironInterface(dll)
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\site-packages\pyomo\common\env.py", line 441, in <genexpr>
    for dll in self.DLLs if dll.available())
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\site-packages\pyomo\common\env.py", line 284, in available
    self._loaded,  self.dll = _load_dll(self._libname)
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\site-packages\pyomo\common\env.py", line 67, in _load_dll
    _load_dll.pool = multiprocessing.Pool(1)
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\context.py", line 119, in Pool
    return Pool(processes, initializer, initargs, maxtasksperchild,
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\pool.py", line 212, in __init__
    self._repopulate_pool()
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\pool.py", line 303, in _repopulate_pool
    return self._repopulate_pool_static(self._ctx, self.Process,
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\pool.py", line 326, in _repopulate_pool_static
    w.start()
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\[REDACTED]\miniconda3\envs\idaes-new\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
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.

Information on your system

Pyomo version: 6.4.0
Python version: 3.9.12
Operating system: Windows 11 Home, 21H2, 22000.613
How Pyomo was installed (PyPI, conda, source): The unholy mixture of Conda and pip from the IDAES advanced user installation. I think Pyomo is ultimately installed via Pip in that procedure.

Additional information

I tried to debug to pin down exactly where additional instances of the script file are called or the script file is imported. The point where the stack trace leaves Pyomo appears to be in pyomo.common.env._load_dll at the lines:

    if _load_dll.pool is None:
        _load_dll.pool = multiprocessing.Pool(1)

It seems like there is some race condition involved here, because 1) Whatever is happening occurs in multiprocessing code and 2) When I was stepping through the code line by line, additional processes weren't spawned.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions