diff --git a/leenkx/blender/lnx/handlers.py b/leenkx/blender/lnx/handlers.py index 54fab49..80cbdb5 100644 --- a/leenkx/blender/lnx/handlers.py +++ b/leenkx/blender/lnx/handlers.py @@ -132,6 +132,7 @@ def always() -> float: return 0.5 + def poll_threads() -> float: """Polls the thread callback queue and if a thread has finished, it is joined with the main thread and the corresponding callback is @@ -141,21 +142,27 @@ def poll_threads() -> float: thread, callback = make.thread_callback_queue.get(block=False) except queue.Empty: return 0.25 - - thread.join() - - try: - callback() - except Exception as e: - # If there is an exception, we can no longer return the time to - # the next call to this polling function, so to keep it running - # we re-register it and then raise the original exception. - bpy.app.timers.unregister(poll_threads) - bpy.app.timers.register(poll_threads, first_interval=0.01, persistent=True) - raise e - - # Quickly check if another thread has finished - return 0.01 + if thread.is_alive(): + try: + make.thread_callback_queue.put((thread, callback), block=False) + except queue.Full: + return 0.5 + return 0.1 + else: + try: + thread.join() + callback() + except Exception as e: + # If there is an exception, we can no longer return the time to + # the next call to this polling function, so to keep it running + # we re-register it and then raise the original exception. + try: + bpy.app.timers.unregister(poll_threads) + except ValueError: + pass + bpy.app.timers.register(poll_threads, first_interval=0.01, persistent=True) + # Quickly check if another thread has finished + return 0.01 loaded_py_libraries: dict[str, types.ModuleType] = {}