AttributeError:由于多重处理,无法腌制本地对象“Flask.__init__.<locals>.<lambda>”

问题描述 投票:0回答:1
我正在尝试在进程内运行网络服务器。 (跟进这个问题为什么我需要使用多处理:

在线程内停止破折号服务器不起作用

这是一个小例子:

import logging from dash import html, Dash from flask import Flask from multiprocessing import Process ############################## setup logger ############################# logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(threadName)s, %(levelname)s] %(message)s", handlers=[ logging.FileHandler("logs.log"), logging.StreamHandler() ] ) ######################################################################### class test: def __init__(self): flask_app = Flask(__name__) self.app = Dash(__name__, server=flask_app) # Define a simple layout self.app.layout = html.Div(children=[ html.H1(children='Hello Dash'), html.P(children='This is a simple Dash app for testing.'), html.Button('Click Me', id='button') ]) def prepare_webserver(self): self.server_proc = Process(target=self.run_server, name='dash_server', daemon=True) self.server_proc.start() # Function to run the server def run_server(self): logging.info("Starting Dash server") self.app.run_server(debug=True, use_reloader=False) if __name__ == "__main__": sim_manager = test() sim_manager.prepare_webserver()
我收到以下错误:
回溯(最近一次调用最后一次):

File "C:\Users\username\Desktop\Projects\python_framework\optimization_framework\test2.py", line 42, in <module> sim_manager.prepare_webserver() File "C:\Users\username\Desktop\Projects\python_framework\optimization_framework\test2.py", line 33, in prepare_webserver self.server_proc.start() File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\multiprocessing\process.py", line 121, in start self._popen = self._Popen(self) File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\multiprocessing\context.py", line 224, in _Popen return _default_context.get_context().Process._Popen(process_obj) File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\multiprocessing\context.py", line 327, in _Popen return Popen(process_obj) File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__ reduction.dump(process_obj, to_child) File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj) AttributeError: Can't pickle local object 'Flask.__init__.<locals>.<lambda>' Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\multiprocessing\spawn.py", line 116, in spawn_main exitcode = _main(fd, parent_sentinel) File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\multiprocessing\spawn.py", line 126, in _main self = reduction.pickle.load(from_parent) EOFError: Ran out of input
    
python flask multiprocessing plotly-dash
1个回答
0
投票
几乎可以肯定

self

 不可 pickle,因此您不能使用 
self.run_server
 作为目标。

尝试将

run_server()

 设置为静态方法。

@staticmethod # Function to run the server def run_server(): server = Test() logging.info("Starting Dash server") server.app.run_server(debug=True, use_reloader=False)
新进程没有理由需要传递经过腌制的现有服务器而不是创建自己的服务器。

© www.soinside.com 2019 - 2024. All rights reserved.