nicegui: Runtime Error when trying to run native=True

Description

I’m getting a Fork vs Spawn error when attempting to run a native app on Ubuntu.

Sample Application (from documentation)

from nicegui import app, ui

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)

Invoked from command line from within a virtual environment: python main.py

Trackback

Traceback (most recent call last):
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/main.py", line 15, in <module>
    main()
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/main.py", line 11, in main
    ui.run(native=True, window_size=(400, 300), fullscreen=False)
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/env/lib/python3.11/site-packages/nicegui/run.py", line 180, in run
    ChangeReload(config, target=globals.server.run, sockets=[sock]).run()
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/env/lib/python3.11/site-packages/uvicorn/supervisors/basereload.py", line 49, in run
    self.startup()
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/env/lib/python3.11/site-packages/uvicorn/supervisors/basereload.py", line 85, in startup
    self.process.start()
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
                  ^^^^^^^^^^^^^^^^^
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/context.py", line 288, in _Popen
    return Popen(process_obj)
           ^^^^^^^^^^^^^^^^^^
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/synchronize.py", line 107, in __getstate__
    raise RuntimeError('A SemLock created in a fork context is being '
RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context. This is not supported. Please use the same context to create multiprocessing objects and Process.

OS: Ubuntu 20.04 Python: 3.11.6 Pyenv: 2.3.29-5-g62e2e9c7 nicegui: 1.3.17 pywebview: 4.3.3

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 4
  • Comments: 23 (6 by maintainers)

Most upvoted comments

Here is how I got it working using venv (Using Arch Linux):

python -m venv venv
source venv/bin/activate
pip install nicegui pywebview qtpy pyqt5 PyQtWebEngine

Here is the code I was able to run without issue.

import multiprocessing
multiprocessing.set_start_method("spawn", force=True)
from nicegui import app, ui

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)

Note: I only saw the error other reported above if:

import multiprocessing
multiprocessing.set_start_method("spawn", force=True)

Came after:

from nicegui import app, ui

Add to the top of your code:

import multiprocessing
multiprocessing.set_start_method("spawn", force=True)

On Linux I also had to pip install qtpy pyqt5 PyQtWebEngine

I faced the exact same error when running with ui.run(native=True) on Ubuntu 24.04 while running inside a python venv in a terminal window. The following worked to fix the error

Step 1: Adding the following lines at the very start of the python script, before from nicegui import ui

import multiprocessing
multiprocessing.set_start_method("spawn", force=True)

Step 2: (I had installed pywebview in the venv with pip install pywebview). I had to additionally do pip install pywebview[qt]

After that, I was able to run the nicegui app as a native app with no weird appearance issues.

For what it’s worth, after closing the native window (by pressing the X in the window titlebar), the following message appears in the console. I don’t know if it is particularly important.

/usr/lib/python3.12/multiprocessing/resource_tracker.py:254: UserWarning: resource_tracker: There appear to be 6 leaked semaphore objects to clean up at shutdown
  warnings.warn('resource_tracker: There appear to be %d '

I managed to get this working.

I’m running Fedora i3 with Nvidia Proprietary drivers and Picom compositor in a venv with python3.11

Here’s what I did:

Update to newest version of nicegui

pip install --upgrade nicegui

Install GTK pygobject

I then got an error that there was no module named ‘gi’ -> See @frankhuurman response above Googling this -> GTK GObject Introspection python bindings are not working so I tried installing these pip install pygobject This did not work for me because of a meson build error.

Install dev tools if above doesn’t work

Double check dependencies are installed - dnf install python3-devel cairo-devel gobject-introspection-devel
If you’re on ubuntu or something else you’ll need to look up the equivalent libraries Make sure to install the correct version of python3-devel (or ubuntu equivalent) for you venv - In my case I was a bit of an idiot because my venv is using 3.11 and system python is 3.13. So for me I had to install sudo dnf install python3.11-devel Meson will fail to find python otherwise After this re-install pip install --force-reinstall --no-cache-dir pycairo pygobject

Additional issues

I also had a GBM buffer issue -> This is GTK nvidia thing. To get it working I had to run WEBKIT_DISABLE_COMPOSITING_MODE=1 GDK_BACKEND=x11 python app.py

Yes, problem persists … but it is a VSCode terminal problem. If you run program using linux standard terminal ( shell ) the problem doesn’t exists. The only warning appears at the end of program execution:

/usr/lib/python3.12/multiprocessing/resource_tracker.py:254: UserWarning: resource_tracker: There appear to be 6 leaked semaphore objects to clean up at shutdown warnings.warn('resource_tracker: There appear to be %d ’

I use Ubuntu 24.04.1, and pywebview 5.3.2 under venv , and I follow the knoppmyth solution .

In VScode environment I installed : pywebview qtpy pyqt5 PyQtWebEngine pycairo PyGObject to try to solve problem but it persists .

A little quirk could be used to solve the first main exception of the program: try to unset the variable GTK_PATH in VSCode terminal, the first exception disappears, but some others problems arises after this quirk.

In VsCode terminal the same program said:

NiceGUI ready to go on http://localhost:8000 [pywebview] Using GTK Gtk-Message: 10:35:45.385: Failed to load module “canberra-gtk-module” /usr/lib/x86_64-linux-gnu/webkit2gtk-4.1/WebKitNetworkProcess: symbol lookup error: /snap/core20/current/lib/x86_64-linux-gnu/libpthread.so.0: undefined symbol: __libc_pthread_init, version GLIBC_PRIVATE Gtk-Message: 10:35:45.649: Failed to load module “canberra-gtk-module” Gtk-Message: 10:35:45.650: Failed to load module “canberra-gtk-module” Gtk-Message: 10:35:45.673: Failed to load module “canberra-gtk-module” Gtk-Message: 10:35:45.675: Failed to load module “canberra-gtk-module” /usr/lib/x86_64-linux-gnu/webkit2gtk-4.1/WebKitNetworkProcess: symbol lookup error: /snap/core20/current/lib/x86_64-linux-gnu/libpthread.so.0: undefined symbol: __libc_pthread_init, version GLIBC_PRIVATE ERROR: WebKit encountered an internal error. This is a WebKit bug. ./Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp(562) : internallyFailedLoadTimerFired

So the problem seems to be located only in the VSCode environment and not in venv environment used by python and requested by Ubuntu .

Here you can see the correct execution of knopmyth snippet under linux shell terminal

Schermata del 2024-11-06 10-46-01

Using:

from nicegui import app, ui
import multiprocessing
multiprocessing.set_start_method("spawn", force=True)

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)

Getting Error:

RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context. This is not supported. Please use the same context to create multiprocessing objects and Process.

Operating system is: Ubuntu 23.10

Installed via: python3 -m pip install pywebview nicegui qtpy pyqt5 PyQtWebEngine

@rodja sorry I somehow forgot to copy that into my post.