Starting a python process in an App using 'spawn' method leads to 'ValueError: bad value(s) in fds_to_keep'. #3594
Replies: 3 comments 3 replies
-
You can solve this by creating a Textual Worker using the @work decorator. By the way: I got the same error when I tried to load pytorch models in a worker, but that's another thing to examine. from textual.app import App
from textual.widgets import Label
from textual import work
from time import sleep
class TestApp(App):
def compose(self):
self.label = Label("Hello World")
yield self.label
def on_mount(self):
self.dummy_proc()
@work(thread=True)
def dummy_proc(self):
sleep(2)
self.label.update("Dummy proc called")
if __name__ == "__main__":
app = TestApp()
app.run() |
Beta Was this translation helpful? Give feedback.
-
Just stumbled onto this error too. I thought there was some fundamental error in my application but it looks like it's just Textual itself. This should be a higher priority fix since it's not possible to even create worker processes (not threads). 3.14 introduces subinterpreters, and I'll be testing it with textual and hope it works.. |
Beta Was this translation helpful? Give feedback.
-
Just queried Gemini 2.5 Pro with this problem, and it suggested wrapping the code in a The only problem is that stdout from those processes can be displayed over your UI, but if that's an acceptable tradeoff for you, then this quick and dirty fix is great. import multiprocessing
from textual.app import App
def dummy_proc():
pass
class TestApp(App):
def on_mount(self):
multiprocessing.set_start_method("spawn")
with self.suspend():
proc = multiprocessing.Process(target=dummy_proc)
proc.start()
proc.join()
if __name__ == "__main__":
app = TestApp()
app.run() (in the OP's example, doing a join() there would block the event loop, so that's not ideal) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I really enjoy this library a lot and find it very useful.
However, today I ran into a problem which I couldn't figure out.
I reduced the code as much as I could to demonstrate the error:
Running this code should lead to "ValueError: bad value(s) in fds_to_keep" in python3.11/multiprocessing/util.py.
I tried this on macos and arch linux.
However, when changing the start method from "spawn" to "fork", this works fine.
Also, just running the block in 'on_mount' on its own runs without problem:
Now, according to the python documentation (https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods, using 'fork' is considered unsafe on macos (which is why I want to use spawn), and the default method on POSIX will change to 'spawn' in python 3.14, so this problem might occur more often in the future.
My guess would be that it is some problem with pickling the main module that leads to the error, but I am not deep enough in the subject to know or know how to fix this.
Does anyone have any clues to what exactly might be happening and if there is a solution (that is not using 'fork')? Thank you!
Beta Was this translation helpful? Give feedback.
All reactions