我尝试通过使用Flask作为Web框架,Pyomo作为优化库以及CBC作为优化引擎来构建用于解决优化问题的Web UI。当我在运行Web服务器时调用求解器时,会出现错误。
如果仅运行优化任务,则不会出错。当与Flask Web服务器一起使用时,似乎会出现问题。
Flask呼叫此行solver = pyomo.SolverFactory('cbc', executable='CBC_PATH')
运行Web服务器时出错:
File "C:\Users\siwapolt\Envs\venv\lib\site-packages\pyomo\opt\base\solvers.py", line 582, in solve
_status = self._apply_solver()
File "C:\Users\siwapolt\Envs\venv\lib\site-packages\pyomo\opt\solver\shellcmd.py", line 244, in _apply_solver
self._rc, self._log = self._execute_command(self._command)
File "C:\Users\siwapolt\Envs\venv\lib\site-packages\pyomo\opt\solver\shellcmd.py", line 308, in _execute_command
define_signal_handlers = self._define_signal_handlers
File "C:\Users\siwapolt\Envs\venv\lib\site-packages\pyutilib\subprocess\processmngr.py", line 545, in run_command
= signal.signal(signal.SIGINT, handler)
File "c:\users\siwapolt\appdata\local\continuum\anaconda3\Lib\signal.py", line 47, in signal
handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread
答案 0 :(得分:0)
是的,只要您拥有PyUtilib 5.6.3,就可以拥有此修复程序。也就是说,信号处理程序默认情况下仍处于启用状态。如果要关闭它,则需要:
import pyutilib.subprocess.GlobalData
pyutilib.subprocess.GlobalData.DEFINE_SIGNAL_HANDLERS_DEFAULT = False
参考:https://github.com/PyUtilib/pyutilib/issues/31#issuecomment-382479024