-
Notifications
You must be signed in to change notification settings - Fork 42
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
Conversation
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.
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 { |
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.
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.
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.
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]] |
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.
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?)
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.
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.
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.
I added docs for this. The callback is run in a separate thread. We decided against supporting async python interfaces in v1.
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.
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?
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.
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]] |
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.
still learning how this stuff is set up, but why do we need 2 definitions of this type?
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.
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.
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.
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 { |
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.
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 :)
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.
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` |
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.
stale comment?
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.
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.
time.sleep(1) | ||
|
||
except KeyboardInterrupt: | ||
server.stop() |
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.
feels like this could be nice to have as a helper method on the server, like sleep_until_interrupted
or something like that?
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.
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.
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.
LG
time.sleep(1) | ||
|
||
except KeyboardInterrupt: | ||
server.stop() |
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.
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, |
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.
Good call on the rename.
ServiceHandler = Callable[["Request"], bytes] | ||
ServiceHandler = Callable[["ServiceRequest"], bytes] | ||
|
||
AssetHandler = Callable[[str], Optional[bytes]] |
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.
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]] |
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.
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 { |
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.
Doesn't need to be pub.
Co-authored-by: Jacob Bandes-Storch <[email protected]>
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.