-
-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We no longer use the `with_executor` mechanism from #11722, as we can directly store an `Executor` on the `PyNailgunClient` cheaply. This allows us to use `Python.allow_threads()` for better parallelism. It's awkward that our Python code now has `PyExecutor` from Rust-Cpython and PyO3, and that those aren't compatible. But this is less offensive than the risk of one big change rather than an incremental migration. We can't yet migrate `PyNailgunSession` because `PyCancellationLatch` is used by `PySession`. -- This also improves our file organization. We use a new pattern of having a `register()` function in each `interface/` file to register that file's functions and classes on the single native extension module, which makes `interface.rs` less bloated.
- Loading branch information
1 parent
3adc0e0
commit 0429258
Showing
10 changed files
with
108 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
64 changes: 64 additions & 0 deletions
64
src/rust/engine/engine_pyo3/src/externs/interface/nailgun.rs
This file contains 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,64 @@ | ||
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
use super::PyExecutor; | ||
use pyo3::create_exception; | ||
use pyo3::exceptions::{PyBrokenPipeError, PyException, PyKeyboardInterrupt}; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyDict; | ||
use task_executor::Executor; | ||
|
||
pub(crate) fn register(py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add( | ||
"PantsdConnectionException", | ||
py.get_type::<PantsdConnectionException>(), | ||
)?; | ||
m.add( | ||
"PantsdClientException", | ||
py.get_type::<PantsdClientException>(), | ||
)?; | ||
m.add_class::<PyNailgunClient>()?; | ||
Ok(()) | ||
} | ||
|
||
create_exception!(native_engine_pyo3, PantsdConnectionException, PyException); | ||
create_exception!(native_engine_pyo3, PantsdClientException, PyException); | ||
|
||
#[pyclass] | ||
struct PyNailgunClient { | ||
port: u16, | ||
executor: Executor, | ||
} | ||
|
||
#[pymethods] | ||
impl PyNailgunClient { | ||
#[new] | ||
fn __new__(port: u16, py_executor: PyExecutor) -> Self { | ||
Self { | ||
port, | ||
executor: py_executor.executor, | ||
} | ||
} | ||
|
||
fn execute(&self, command: String, args: Vec<String>, env: &PyDict, py: Python) -> PyResult<i32> { | ||
use nailgun::NailgunClientError; | ||
|
||
let env_list: Vec<(String, String)> = env | ||
.items() | ||
.into_iter() | ||
.map(|kv_pair| kv_pair.extract::<(String, String)>()) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
|
||
py.allow_threads(|| { | ||
self | ||
.executor | ||
.block_on(nailgun::client_execute(self.port, command, args, env_list)) | ||
.map_err(|e| match e { | ||
NailgunClientError::PreConnect(err_str) => PantsdConnectionException::new_err(err_str), | ||
NailgunClientError::PostConnect(err_str) => PantsdClientException::new_err(err_str), | ||
NailgunClientError::BrokenPipe => PyBrokenPipeError::new_err(""), | ||
NailgunClientError::KeyboardInterrupt => PyKeyboardInterrupt::new_err(""), | ||
}) | ||
}) | ||
} | ||
} |
This file contains 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 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