-
Notifications
You must be signed in to change notification settings - Fork 939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating threads to update visualization asynchronously #2656
base: main
Are you sure you want to change the base?
Conversation
execution is slower,thread not stopping gracefully
pending- eipstein not rendering prply(check other models) stopping thread(use_thread) graph inaccuracy
implementing bi-directional threads is ensuring synchronization between model execution and visualization, though this is resulting in reduced performance.
takes care of valueError and CancelledError. pending changes:clean code
optimal performance in browser and jupyter
In use_threads, the threads for step and vis may compete for GI leading to slower execution, especially when sharing resources. In contrast, use_task, which is asyncio-based, avoids GIL contention.
for more information, see https://pre-commit.ci
Performance benchmarks:
|
Thanks for the PR and the detailed PR message! I’m on vacation the whole of February, so I will leave the review to other maintainers. Have you also tried using a separate thread for every figure? |
Thanks! Hope you have a great vacation! |
…control This should improve the overall functionality and reliability of the SimulatorController, particularly in scenarios where stopping the execution is necessary.
That's an excellent point. You're right. Another option could be that a new thread is used to draw all figures for a single step. Then the figures for a certain step will be ready at the same time. You would have to guarantee that each frame is displayed for about the same time (or for a minimum duration). It's up to you how complicated you make it in this PR. We could also keep this PR to a minimal, simple implementation and implement further ideas in future PRs. |
Yes, this PR ensures a single thread handles drawing all figures together for a specific step, ensuring synchronization.
Noted, currently even if the play interval is set, the model doesn’t always visualize it correctly, and the user has to manually adjust it leading to poor responsiveness.I'll start working on this and create a new PR to handle this issue. |
Summary
This PR introduces a thread-based mechanism to handle model updates and visualization rendering concurrently. It ensures smooth execution of simulations by separating model step execution and visualization updates into independent threads, improving performance and responsiveness during simulations.
Fixes #2604
Motive
Previously, the visualization process could become a bottleneck during rapid simulations, as rendering was tightly coupled with model updates. This caused delays and UI responsiveness issues. By separating these operations into threads, the model execution is no longer hindered by the visualization process.
Implementation
Handles visualization updates triggered by threading.Event (visualization_pause_event) to synchronize rendering with model steps.
Implemented visualization_pause_event to signal the visualization thread after each model step is completed, ensuring rendering happens efficiently without blocking the simulation.
Usage Examples
Play Button Behavior: The play button now ensures the simulation continues smoothly.
Concurrent Rendering: Visualization and simulation run in parallel, reducing delays.
Before
![Screenshot 2025-01-10 200323](https://private-user-images.githubusercontent.com/166975398/408456114-43f54915-2ec7-42ef-a81a-668bd0a1f10a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NzMwMDIsIm5iZiI6MTczODg3MjcwMiwicGF0aCI6Ii8xNjY5NzUzOTgvNDA4NDU2MTE0LTQzZjU0OTE1LTJlYzctNDJlZi1hODFhLTY2OGJkMGExZjEwYS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwNlQyMDExNDJaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT03Yjg3YjA0NGQ1ZGFkMGJlNDI0M2UxMzBhZWMwMGQyNGE5NzMxMzhlOTExNmEyYjQ3MGZjODQ5NzgzMmRiN2VjJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.0J55SdCn5YQwkRm1qyguvxZ2auOPUcuuxy_Zv3DMvEw)
After
![Screenshot 2025-01-31 130506](https://private-user-images.githubusercontent.com/166975398/408457235-a13b5cf5-03c5-4a54-998c-70bce7a93a49.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NzMwMDIsIm5iZiI6MTczODg3MjcwMiwicGF0aCI6Ii8xNjY5NzUzOTgvNDA4NDU3MjM1LWExM2I1Y2Y1LTAzYzUtNGE1NC05OThjLTcwYmNlN2E5M2E0OS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwNlQyMDExNDJaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xZDBiNDE0YjY4YmJlM2I4MzVjYjMyZjQwZmFkZGY2N2ExODU5NThiNGM1YmVjZDYwZmRiNDYyMDJmNjQ5YjliJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.JmU3kYZzmghBTNWhDGitVYgjB5OHcY4R0Kh3sKBcN7A)
Additional Notes