Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import rosys does not work with ui.run(reload=False) - RuntimeError: multiprocessing start method must be "spawn" #19

Open
AndyPermaRobotics opened this issue Aug 16, 2023 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@AndyPermaRobotics
Copy link
Contributor

Hello everyone,

I have come across the following error message when I set the reload flag to False in the ui.run function:

 Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/nicegui/background_tasks.py", line 55, in _handle_task_result
  task.result()
File "/usr/local/lib/python3.11/site-packages/nicegui/helpers.py", line 77, in result_with_client
  await result
File "/rosys/rosys/rosys.py", line 144, in startup
  raise RuntimeError(
RuntimeError: multiprocessing start method must be "spawn"; see https://pythonspeed.com/articles/python-multiprocessing/

To better localize the source of the error, I have created a minimal example where the error occurs. Just import rosys is enough.

Example:

from nicegui import ui

import rosys

ui.run(
    title='Test', 
    port=8080,
    reload=False
    )

The error also occurs when I try to plan a path in my main program.


How did I come up with the idea of setting reload=False?

The reload flag causes my script to run twice. This results in two instances of each class being created. This has caused problems when I tried to subscribe to an MQTT topic, and my callback function was suddenly called twice for each message. The reload does not seem to be a complete restart where all resources from the previous run are cleaned up.

@rodja
Copy link
Member

rodja commented Aug 16, 2023

Thanks for reporting this @AndyPermaRobotics, but the example works fine here on MacOS. Are you testing this on Windows?

About the multiple evaluation of main you may want to have a look into the NiceGUI FAQ where I just added the infos from zauberzeug/nicegui#794.

In RoSys we often use the last option with app.on_startup in combination with a System class which manages the robot state. See our field friend code for example.

@AndyPermaRobotics
Copy link
Contributor Author

@rodja thanks for the link to the FAQ, my script works now.

It's interesting, that you could not reproduce the issue on MacOS.
I let it run in a Docker Container.

Dockerfile:


FROM zauberzeug/rosys:latest 

WORKDIR /usr/src/app

docker-compose.yml

version: "3.9"
services:
  rosys_service:
    build: .
    tty: true
    ports:
      - "8080:8080"
    volumes:
      - ./:/app
    hostname: docker

@rodja
Copy link
Member

rodja commented Aug 19, 2023

Ah very good @AndyPermaRobotics. With Docker the error comes up as described by you. Before I had just tested locally on mac.

@rodja rodja added the bug Something isn't working label Aug 19, 2023
@falkoschindler falkoschindler added this to the 0.9.2 milestone Sep 29, 2023
@falkoschindler
Copy link
Contributor

@rodja Do you already have an idea what to do about it?

@rodja
Copy link
Member

rodja commented Sep 30, 2023

No, I have not looked into it.

@falkoschindler falkoschindler modified the milestones: 0.9.2, 0.9.3 Oct 11, 2023
@rodja rodja modified the milestones: 0.9.3, 0.9.4 Nov 3, 2023
@rodja rodja modified the milestones: 0.9.5, 0.9.6 Nov 26, 2023
@rodja rodja modified the milestones: 0.9.6, 0.9.7 Dec 22, 2023
@NiklasNeugebauer
Copy link
Contributor

I looked into this a little. I think the best way to solve this may be keeping Nicegui's behavior consistent in case there is no specific reason that it does not set the spawn method when reload=False. However, I do not understand enough about Nicegui to fix this.

Another Option would be forcing the method to spawn instead of raising an exception in rosys.py on_startup:
multiprocessing.set_start_method('spawn', force=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants