Threading in python #7440
-
|
Can someone point me to anything in the documentation showing which functions do not execute in the main thread? There are several mentions in the docs/examples of the restriction on updating the GUI from non-min threads, but most specific examples were deliberately contrived to demonstrate the post_to_main_thread() function. Only the callback designated by render_to_depth_image(() is explicitly called out as executing in its own thread, and I discovered (the hard way) the callbacks from file dialogs also seem to do so. But I'd love to find some more elegant way to discover any others than by crashing the GUI. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Hi @lee-deraud! For Open3D GUI operations, you're dealing with thread-safety constraints similar to most GUI frameworks. Here's what you need to know: Thread-Safe ApproachInstead of trying to find which functions are thread-safe, I'd recommend using Open3D's built-in threading model: import open3d as o3d
def gui_update_callback():
# Your GUI updates here
# This runs safely in the main thread
return True # Return False to stop
# In your main thread
app = o3d.visualization.gui.Application.instance
app.post_to_main_thread(window, gui_update_callback)General Pattern for Threaded Operationsfrom threading import Thread
import open3d as o3d
class MyVisualizer:
def __init__(self):
self.app = o3d.visualization.gui.Application.instance
self.window = self.app.create_window("My Window", 1024, 768)
def background_work(self):
# Heavy computation in background thread
result = self.process_point_cloud()
# Post result back to main thread for GUI update
self.app.post_to_main_thread(
self.window,
lambda: self.update_gui(result)
)
def update_gui(self, result):
# All GUI operations here are safe
passKey Guidelines
This pattern is more reliable than trying to determine which specific functions are thread-safe, as it can change between versions. Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hi @lee-deraud,
That’s a very fair question — and you’re not missing anything in the docs.
In Open3D’s GUI layer, the only code that is reliably on the main thread is:
• Event callbacks you register on widgets (button clicks, sliders, menu items, etc.)
• The on_layout, on_draw, and similar window lifecycle callbacks
• Anything you explicitly dispatch via post_to_main_thread()
Everything else should be treated as potentially non-main-thread unless explicitly documented.
In particular:
• render_to_depth_image() is documented as threaded
• File dialog callbacks are indeed invoked from a worker thread
• Some async rendering / IO utilities may also not return on the UI thread
Unfortunately, th…