-
-
Notifications
You must be signed in to change notification settings - Fork 3
feat(flight): KML trajectory export endpoint #73
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
Open
aasitvora99
wants to merge
3
commits into
enh/tank-geometry-fluid-density
Choose a base branch
from
enh/flight-kml-export
base: enh/tank-geometry-fluid-density
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Performance & Scalability | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: RocketPy-Team/Infinity-API
Length of output: 13835
🏁 Script executed:
Repository: RocketPy-Team/Infinity-API
Length of output: 7661
🌐 Web query:
RocketPy Python FlightDataExporter export_kml source Thread safe💡 Result:
In RocketPy, the
FlightDataExporterclass is the recommended way to export simulation data, including KML files for trajectory visualization [1][2]. Theexport_kmlmethod is accessed by instantiatingFlightDataExporterwith aFlightobject and calling the method with the desired parameters, such asfile_nameandaltitude_mode[3][1]. Regarding thread safety, theFlightDataExporter.export_kmlmethod is not explicitly documented as thread-safe. RocketPy's general architecture, particularly its core simulation classes, is not designed with inherent thread safety for concurrent access to the same object instance [4][5]. While theMonteCarlosimulation engine supports parallel execution, it achieves this by distributing independentFlightinstances across separate processes using Python'smultiprocessingmodule rather than by sharing state across threads [4][5]. If you need to perform concurrent exports, it is safer to ensure that each thread or process operates on its own dedicatedFlightDataExporterinstance and correspondingFlightobject to avoid race conditions or data corruption [4][5].Citations:
Offload KML generation from the event loop.
get_flight_kml()holds the FastAPI event loop while constructing a RocketPyFlight, running the synchronousFlightDataExporter.export_kml()path, and reading/writing temp files. Run the export in a bounded worker pool, such asasyncio.to_thread, so concurrent downloads don’t block unrelated requests. If export state is shared perFlightDataExporter, ensure the export operates on the dedicated exporter/flight instance in that worker.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure, my memory on the event loop is lacking, but IIRC FastAPI already handles everything async anyway , it will just wrap the sync work in a thread inside the pool and let it compete in scheduling with everything else, can you confirm @coderabbitai ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🤖 web_search executed:
FastAPI documentation async def synchronous code called inside async path operation function threadpool Starlette run_in_threadpoolLength of output: 20825
@GabrielBarberini, confirmed by the FastAPI docs: it automatically runs a path operation declared with ordinarydefin an external thread pool, but it does not automatically offload synchronous utility calls made from anasync defhandler.Here,
get_flight_kml()isasync def, so the direct synchronous call:runs on the event-loop thread and blocks it until RocketPy finishes exporting and the temporary-file I/O completes. Explicitly awaiting
asyncio.to_thread(...)orfastapi.concurrency.run_in_threadpool(...)is the appropriate fix.The docs also explicitly note that this automatic thread-pool behavior applies to synchronous path operations and dependencies—not arbitrary synchronous functions invoked within an async path operation: https://fastapi.tiangolo.com/async/.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai what would you suggest then ? should we declare this route in specific as sync (def) or keep with it async ? what would you do in case we keep it async to solve that problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GabrielBarberini, I’d keep the route and controller async. They already await database work (get_flight_by_id), so changing this endpoint todefwould require awkwardly bridging back into async code and provides no benefit.Offload only the blocking RocketPy export call to FastAPI/Starlette’s managed thread pool:
This keeps database access on the async path while moving the synchronous RocketPy processing and temporary-file I/O off the event-loop thread. The
FlightServiceis local to this request, so the worker operates on a dedicated service/exporter instance.asyncio.to_thread(flight_service.get_flight_kml)would also work, but I’d preferrun_in_threadpool(...)in this FastAPI application because it uses Starlette/AnyIO’s managed worker-pool behavior.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aasitvora99 , @Gui-FernandesBR , I think
run_in_threadpool(flight_service.get_flight_kml)seems to be a clean solution, wdyt?