-
Notifications
You must be signed in to change notification settings - Fork 750
feat(request cancellation): pycontext, propagating the is_stopped into python land.
#2158
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c96e96c
add test pycontext
michaelfeil cefe9ea
Merge branch 'main' into mf/pycontext-cancellation
michaelfeil 22079d1
fmt
michaelfeil 9fec8ab
fmt
michaelfeil 72d8031
fmt
michaelfeil 0e9943d
fmt
michaelfeil 2ca3285
fmt - clippy
michaelfeil eff7900
add header
michaelfeil 12afaef
Merge branch 'main' into mf/pycontext-cancellation
kthui ce3677e
Merge branch 'main' into mf/pycontext-cancellation
michaelfeil d96bb8e
cargo fmt
michaelfeil 87ee8c7
move inspect into rust
michaelfeil e82279b
cliipy fix
michaelfeil e1668fc
check hello world
michaelfeil 4491738
fix pycontext
michaelfeil 3b26624
fix black
michaelfeil 5071bd8
rename to pycontext
michaelfeil 3e38b73
Merge branch 'main' into mf/pycontext-cancellation
michaelfeil 7238cb4
fix the pre-commit again..
michaelfeil ef6ee61
fix the pre-commit again.. x2
michaelfeil 2e69d5b
fix the pre-commit again.. x2
michaelfeil 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // PyContext is a wrapper around the AsyncEngineContext to allow for Python bindings. | ||
|
|
||
| pub use dynamo_runtime::pipeline::AsyncEngineContext; | ||
| use pyo3::prelude::*; | ||
| use std::sync::Arc; | ||
|
|
||
| // PyContext is a wrapper around the AsyncEngineContext to allow for Python bindings. | ||
| // Not all methods of the AsyncEngineContext are exposed, jsut the primary ones for tracing + cancellation. | ||
| // Kept as class, to allow for future expansion if needed. | ||
| #[pyclass] | ||
| pub struct PyContext { | ||
| pub inner: Arc<dyn AsyncEngineContext>, | ||
| } | ||
|
|
||
| impl PyContext { | ||
| pub fn new(inner: Arc<dyn AsyncEngineContext>) -> Self { | ||
| Self { inner } | ||
| } | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl PyContext { | ||
| // sync method of `await async_is_stopped()` | ||
| fn is_stopped(&self) -> bool { | ||
| self.inner.is_stopped() | ||
| } | ||
|
|
||
| // sync method of `await async_is_killed()` | ||
| fn is_killed(&self) -> bool { | ||
| self.inner.is_killed() | ||
| } | ||
| // issues a stop generating | ||
| fn stop_generating(&self) { | ||
| self.inner.stop_generating(); | ||
| } | ||
|
|
||
| fn id(&self) -> &str { | ||
| self.inner.id() | ||
| } | ||
|
|
||
| // allows building a async callback. | ||
| fn async_killed_or_stopped<'a>(&self, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> { | ||
| let inner = self.inner.clone(); | ||
|
|
||
| pyo3_async_runtimes::tokio::future_into_py(py, async move { | ||
| tokio::select! { | ||
| _ = inner.killed() => { | ||
| Ok(true) | ||
| } | ||
| _ = inner.stopped() => { | ||
| Ok(true) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // PyO3 equivalent for verify if signature contains target_name | ||
| // def callable_accepts_kwarg(target_name: str): | ||
| // import inspect | ||
| // return target_name in inspect.signature(func).parameters | ||
| pub fn callable_accepts_kwarg( | ||
| py: Python, | ||
| callable: &Bound<'_, PyAny>, | ||
| target_name: &str, | ||
| ) -> PyResult<bool> { | ||
| let inspect: Bound<'_, PyModule> = py.import("inspect")?; | ||
| let signature = inspect.call_method1("signature", (callable,))?; | ||
| let params_any: Bound<'_, PyAny> = signature.getattr("parameters")?; | ||
| params_any | ||
| .call_method1("__contains__", (target_name,))? | ||
| .extract::<bool>() | ||
| } | ||
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.