Repository navigation
Startup procesure when application uses a multiprocessing.Manager #6501
First Check
Example Codeimport multiprocessing
from multiprocessing import Manager
from nicegui import ui
class Model:
def __init__(self):
print('Creating manager')
self.manager = Manager()
if __name__ in {"__main__", "__mp_main__"}:
multiprocessing.set_start_method('spawn', force=True)
model = Model()
@ui.page('/')
def index():
ui.label('Hello')
ui.run(reload=True)DescriptionI get the following error:
What is the proper startup logic to avoid this error? NiceGUI Version4.0.0 Python Version3.14.6 BrowserFirefox Operating SystemLinux Additional ContextNo response |
Replies: 1 comment
|
Hi @Pibben, With The from multiprocessing import Manager
from nicegui import app, ui
manager = None
def create_manager():
global manager
manager = Manager()
app.on_startup(create_manager)
app.on_shutdown(lambda: manager.shutdown())
@ui.page('/')
def index():
ui.label(f'Hello {manager.dict()}')
if __name__ in {"__main__", "__mp_main__"}:
ui.run(reload=True)You don't need |
Hi @Pibben,
With
reload=True, uvicorn starts the server worker with the "spawn" method, and this child process re-runs your main script as__mp_main__while it is still bootstrapping. Because of theif __name__ in {"__main__", "__mp_main__"}:guard,Model()runs there too, andManager()tries to start yet another process before the worker has finished starting up. That's exactly what Python complains about. (You can also see "Creating manager" printed twice: once in the reloader process, which only watches your files, and once in the worker.)The
__mp_main__part of the guard is only meant forui.run, which returns immediately in child processes. Expensive side effects like starting proce…