pthom / imgui_bundle

Dear ImGui Bundle: an extensive set of Ready-to-use widgets and libraries, based on ImGui. Start your first app in 5 lines of code, or less. Whether you prefer Python or C++, this pack has your back!
https://pthom.github.io/imgui_bundle/
MIT License
592 stars 63 forks source link

Python socket communication working on separate thread slows down when imgui bundle running in parel. #124

Closed SebastianMilosz closed 8 months ago

SebastianMilosz commented 11 months ago

We are using Hello Imgui from imgui bundle in our Python application. In a separate thread we have code that use sockets for receiving some data in the loop. We have notice that this thread significantly slows down when we have gui running in parel

pthom commented 9 months ago

Hi,

As you probably know, due to the GIL (Global Interpreter Lock), only one thread can execute Python code at the same time. Since ImGui is an immediate Gui mode library, it refreshes the screen from scratch at each frame. This is fast, however in python's multithreaded mode, this might slow down other threads.

As mentioned in the threading page for python, it would be advisable to use multiprocessing instead. When using multiprocessing, python provides a dedicated message queue, which is quite efficient and expressive.

Anyhow, you might want to reduce the FPS of the app when idling, see hello_imgui.RunnerParams.fpg_idling

class FpsIdling:
    """*
     @@md#FpsIdling

    **FpsIdling** is a struct that contains Fps Idling parameters

    * `fpsIdle`: _float, default=9_.
      ImGui applications can consume a lot of CPU, since they update the screen very frequently.
      In order to reduce the CPU usage, the FPS is reduced when no user interaction is detected.
      This is ok most of the time but if you are displaying animated widgets (for example a live video),
      you may want to ask for a faster refresh: either increase fpsIdle, or set it to 0 for maximum refresh speed
      (you can change this value during the execution depending on your application refresh needs)
    * `enableIdling`: _bool, default=true_.
      Set this to False to disable idling (this can be changed dynamically during execution)
    * `isIdling`: bool (dynamically updated during execution)
      This bool will be updated during the application execution, and will be set to True when it is idling.
    * `rememberEnableIdling`: _bool, default=true_.
      If True, the last value of enableIdling is restored from the settings at startup.
    @@md

    """
    # float fpsIdle = 9.f;    /* original C++ signature */
    fps_idle: float = 9.
    # bool  enableIdling = true;    /* original C++ signature */
    enable_idling: bool = True
    # bool  isIdling = false;    /* original C++ signature */
    is_idling: bool = False
    # bool  rememberEnableIdling = true;    /* original C++ signature */
    remember_enable_idling: bool = True
pthom commented 5 months ago

Hi,

Speed should now be improved when running multithreaded code: see related issue with the GIL