首页 > 解决方案 > 从 .views 文件启动新进程时,Flask 应用程序中的 RuntimeError “freeze_support”

问题描述

我正在开发一个 Flask 应用程序。由于服务器要不断地向接收者发送信息,因此每个视图路由调用都需要调用一个函数来控制并行运行进程的控制队列。

整个错误信息:

C:\Users\fabik\PycharmProjects\LEDStripControlWebapp\venv\Scripts\python.exe C:/Users/fabik/PycharmProjects/LEDStripControlWebapp_ErrorRecreation/main.py
 * Serving Flask app "website" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Program Files\Python39\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "C:\Program Files\Python39\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Program Files\Python39\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "C:\Program Files\Python39\lib\runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Program Files\Python39\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\fabik\PycharmProjects\LEDStripControlWebapp_ErrorRecreation\main.py", line 3, in <module>
    app = create_app()
  File "C:\Users\fabik\PycharmProjects\LEDStripControlWebapp_ErrorRecreation\website\__init__.py", line 7, in create_app
    from .views import views
  File "C:\Users\fabik\PycharmProjects\LEDStripControlWebapp_ErrorRecreation\website\views.py", line 8, in <module>
    control.start_loop()
  File "C:\Users\fabik\PycharmProjects\LEDStripControlWebapp_ErrorRecreation\control\control.py", line 10, in start_loop
    self._process.start()
  File "C:\Program Files\Python39\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Program Files\Python39\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Program Files\Python39\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Program Files\Python39\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Program Files\Python39\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "C:\Program Files\Python39\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.

(运行 main.py 文件时,我实际上收到了两次此消息)

不知道出了什么问题。

项目目录

main.py 文件:

from website import create_app

app = create_app()

if __name__ == "__main__":
    app.run(debug=True)

初始化.py 文件:

from flask import Flask

def create_app():
    app = Flask(__name__)
    app.config["SECRET_KEY"] = "thisstringisveryrandom"

    from .views import views

    app.register_blueprint(views, url_prefix="/")

    return app

视图.py 文件:

from flask import Blueprint, render_template, request

from control.control import Control

views = Blueprint("views", __name__)

control = Control()
control.start_loop()

@views.route("/", methods=["GET", "POST"])
def home():
    if request.method == "GET":
        pass

    if request.method == "POST":
        pass

    return render_template("base.html")

control.py 文件:

from multiprocessing import Queue, Process

class Control:
    def __init__(self):
        self._control_queue = Queue()
        self._process = Process(target=self._loop)
        self._process.daemon = True

    def start_loop(self):
        self._process.start()

    @staticmethod
    def _loop(control_queue, connection):
        while True:
            #this reacts to control_queue
            pass

标签: pythonflaskservermultiprocessing

解决方案


推荐阅读