Skip to content

python: support asset serving #259

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

Merged
merged 13 commits into from
Feb 28, 2025

Conversation

bryfox
Copy link
Contributor

@bryfox bryfox commented Feb 28, 2025

This adds support for an asset handler in the Python SDK. It follows the general approach of services: we expect a single function that returns the bytes of the asset, and call this function in a separate thread.

I also renamed "Request" -> "ServiceRequest" here. Even though the asset request is only a single URI, I think this clarifies what the request is for. It also localizes classes in the documentation.

I added a basic example which will serve "package://" assets from the local directory. We could include URDF descriptions in the example itself to make this easier to see out of the box; I haven't done so yet.

Copy link

linear bot commented Feb 28, 2025

@bryfox bryfox requested review from gasmith and eloff February 28, 2025 18:16
Copy link
Contributor

@eloff eloff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, maybe one possibility to simplify a little, but don't spend too long on it

/// asset `bytes`. If the handler returns `None`, a "not found" message will be sent to the client.
/// If the handler raises an exception, the stringified exception message will be returned to the
/// client as an error.
pub struct AssetServer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be simpler if you structured it as a closure passed as the blocking callback (if we're not using the builder you'd need to construct it yourself to set it in server options.)

At least it does the spawning and arc for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling to come up with the conversion for that. I'll merge this for now and take another look when I flesh out the URDF asset example.

ServiceHandler = Callable[["Request"], bytes]
ServiceHandler = Callable[["ServiceRequest"], bytes]

AssetHandler = Callable[[str], Optional[bytes]]
Copy link
Member

@jtbandes jtbandes Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like we need some explanation of what slow/async implementations are expected to do. What if the user wants an implementation that returns Awaitable? is that allowed? Any threading notes we need to add?

(Motivating example: how do I fit a slow network call into this API?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In python, you can do whatever you want, which is why we spawn the callback on one of tokio's blocking threads. They can block as long as they want without causing any harm.

I don't have much experience with async python - and I get the impression it's not widely used - but if you really want to invoke an async function from the asset handler callback, I presume there's a way to get a handle to the asyncio eventloop and call loop.run(my_async_fn) to block and wait for it to complete.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added docs for this. The callback is run in a separate thread. We decided against supporting async python interfaces in v1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can block as long as they want without causing any harm.

If they invoke something like a network call, will that result in the GIL being unlocked during the network call?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, just the same as if they had made the network call from any other context.

@@ -180,7 +180,9 @@ AnyParameterValue = Union[
ParameterValue.Dict,
]

class Request:
AssetHandler = Callable[[str], Optional[bytes]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still learning how this stuff is set up, but why do we need 2 definitions of this type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about that too. Feels like we should be able to do from .. import AssetHandler, ServiceHandler or so, to pull the type definition from the parent module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment in the public module. Normally, that file imports everything from this stub, but we need to re-define the types for sphnix docs to work. I think this stub could probably import from the public file, but I don't think it should.

@@ -356,6 +360,12 @@ pub fn start_server(
server = server.services(services.into_iter().map(PyService::into));
}

if let Some(asset_handler) = asset_handler {
server = server.fetch_asset_handler(Box::new(AssetServer {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming wise, the "AssetServer" name makes me think this is a standalone thing and not something I would necessarily expect to be passed in as an "asset handler"

could we call it something with AssetHandler in the name (I see AssetHandler is taken though) – PyAssetHandler? CallbackAssetHandler?

I guess using @eloff's suggestion of a closure would also resolve this concern because it would no longer need a name :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to CallbackAssetHandler for now; I'll see if I can figure out conversions when I revisit the examples. This name isn't exposed to the python side, so I'd like to avoid the Py prefix.


try:
while True:
# Send transforms for the model as needed, on a `FrameTransformsChannel`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stale comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a placeholder instruction to the user; they'll need to publish the frame transforms.

I'm going to follow up with a more complete example, at which point I'll remove this.

Comment on lines +36 to +39
time.sleep(1)

except KeyboardInterrupt:
server.stop()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels like this could be nice to have as a helper method on the server, like sleep_until_interrupted or something like that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it might be nice to have that sugar for these examples, I don't think it'd find much practical use in the real world. Most users of this SDK are going to start the server, maybe register server.stop() as a pyexit hook if they're feeling fastidious, and then go about their actual business.

Copy link
Contributor

@gasmith gasmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG

Comment on lines +36 to +39
time.sleep(1)

except KeyboardInterrupt:
server.stop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it might be nice to have that sugar for these examples, I don't think it'd find much practical use in the real world. Most users of this SDK are going to start the server, maybe register server.stop() as a pyexit hook if they're feeling fastidious, and then go about their actual business.

Schema,
Service,
ServiceRequest,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call on the rename.

ServiceHandler = Callable[["Request"], bytes]
ServiceHandler = Callable[["ServiceRequest"], bytes]

AssetHandler = Callable[[str], Optional[bytes]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In python, you can do whatever you want, which is why we spawn the callback on one of tokio's blocking threads. They can block as long as they want without causing any harm.

I don't have much experience with async python - and I get the impression it's not widely used - but if you really want to invoke an async function from the asset handler callback, I presume there's a way to get a handle to the asyncio eventloop and call loop.run(my_async_fn) to block and wait for it to complete.

@@ -180,7 +180,9 @@ AnyParameterValue = Union[
ParameterValue.Dict,
]

class Request:
AssetHandler = Callable[[str], Optional[bytes]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about that too. Feels like we should be able to do from .. import AssetHandler, ServiceHandler or so, to pull the type definition from the parent module.

/// asset `bytes`. If the handler returns `None`, a "not found" message will be sent to the client.
/// If the handler raises an exception, the stringified exception message will be returned to the
/// client as an error.
pub struct AssetServer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need to be pub.

Co-authored-by: Jacob Bandes-Storch <[email protected]>
@bryfox bryfox merged commit e0aa193 into main Feb 28, 2025
36 checks passed
@bryfox bryfox deleted the bryan/fg-10666-support-asset-service-in-python branch February 28, 2025 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants