Open axeldavy opened 2 months ago
The following variant doesn't hang:
import dearpygui.dearpygui as dpg
import time
dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()
with dpg.window(tag="Primary Window"):
dpg.add_text("Hello", tag="hello")
def hanging_handler(unused):
with dpg.mutex():
time.sleep(0.01)
dpg.show_viewport()
dpg.set_primary_window("Primary Window", True)
for i in range(20):
dpg.set_frame_callback(i+1, hanging_handler)
t0 = time.time()
while(dpg.is_dearpygui_running()):
t1 = time.time()
print("Frame rendered in:", t1-t0)
t0 = t1
dpg.render_dearpygui_frame()
dpg.destroy_context()
Thus it might be possible that the hang calls from set_frame_callback not being thread-safe.
The first script hangs up on dpg.get_frame_count()
. Here's why.
set_frame_callback
schedules the callback to run on the handlers thread, as it should.time.sleep
, at which point Python releases the GIL and lets another thread take over. The mutex remains locked.get_frame_count
, that function attempts to lock the mutex. Note: among the API functions called in the rendering loop, only render_dearpygui_frame
and get_frame_count
actually lock the mutex; the other two calls don't do it.sleep
is over, the handlers thread wakes up and needs to lock the GIL, which is held by the main thread.I think I've fixed this kind of deadlocks in my copy of DPG and will eventually push the fix to the main repo, but this won't happen soon. Until that point, avoid making extra API calls from the rendering loop. The default rendering loop (consisting of is_dearpygui_running
and render_dearpygui_frame
) does not deadlock.
Version of Dear PyGui
Version: 1.11.1 Operating System: Arch Linux
My Issue/Question
When doing heavy processing inside a dpg.mutex in a callback called from set_frame_callback, the application hangs.
To Reproduce
Run:
This doesn't occur without the dpg.mutex or from a mouse or keyboard callback. The following code doesn't hang: