-
Notifications
You must be signed in to change notification settings - Fork 17
Release the GIL during certain operations to allow other threads to run #372
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @wkrettek, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the concurrency of the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request correctly releases the Python GIL during blocking I/O operations, which should improve performance in multi-threaded applications. The changes are applied consistently across various methods like query, execute, and connect. I've included one suggestion to refactor some duplicated code in the Connection methods to improve maintainability.
| pub fn query(&self, sql: &str) -> PyResult<TaosResult> { | ||
| match self.current_cursor()?.query(sql) { | ||
| Ok(rs) => { | ||
| let cols = rs.num_of_fields(); | ||
| Ok(TaosResult { | ||
| _inner: rs, | ||
| _block: None, | ||
| _current: 0, | ||
| _num_of_fields: cols as _, | ||
| _tz: self._tz, | ||
| }) | ||
| } | ||
| Err(err) => Err(QueryError::new_err(err.to_string())), | ||
| } | ||
| Python::with_gil(|py| { | ||
| let taos = self.current_cursor()?; | ||
| let rs = py.allow_threads(|| { | ||
| taos.query(sql) | ||
| }).map_err(|err| QueryError::new_err(err.to_string()))?; | ||
|
|
||
| let cols = rs.num_of_fields(); | ||
| Ok(TaosResult { | ||
| _inner: rs, | ||
| _block: None, | ||
| _current: 0, | ||
| _num_of_fields: cols as _, | ||
| _tz: self._tz, | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| pub fn query_with_req_id(&self, sql: &str, req_id: u64) -> PyResult<TaosResult> { | ||
| match self.current_cursor()?.query_with_req_id(sql, req_id) { | ||
| Ok(rs) => { | ||
| let cols = rs.num_of_fields(); | ||
| Ok(TaosResult { | ||
| _inner: rs, | ||
| _block: None, | ||
| _current: 0, | ||
| _num_of_fields: cols as _, | ||
| _tz: self._tz, | ||
| }) | ||
| } | ||
| Err(err) => Err(QueryError::new_err(err.to_string())), | ||
| } | ||
| Python::with_gil(|py| { | ||
| let taos = self.current_cursor()?; | ||
| let rs = py.allow_threads(|| { | ||
| taos.query_with_req_id(sql, req_id) | ||
| }).map_err(|err| QueryError::new_err(err.to_string()))?; | ||
|
|
||
| let cols = rs.num_of_fields(); | ||
| Ok(TaosResult { | ||
| _inner: rs, | ||
| _block: None, | ||
| _current: 0, | ||
| _num_of_fields: cols as _, | ||
| _tz: self._tz, | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| pub fn execute(&self, sql: &str) -> PyResult<i32> { | ||
| match self.current_cursor()?.query(sql) { | ||
| Ok(rs) => Ok(rs.affected_rows()), | ||
| Err(err) => Err(QueryError::new_err(err.to_string())), | ||
| } | ||
| Python::with_gil(|py| { | ||
| let taos = self.current_cursor()?; | ||
| let rs = py.allow_threads(|| { | ||
| taos.query(sql) | ||
| }).map_err(|err| QueryError::new_err(err.to_string()))?; | ||
| Ok(rs.affected_rows()) | ||
| }) | ||
| } | ||
|
|
||
| pub fn execute_with_req_id(&self, sql: &str, req_id: u64) -> PyResult<i32> { | ||
| match self.current_cursor()?.query_with_req_id(sql, req_id) { | ||
| Ok(rs) => Ok(rs.affected_rows()), | ||
| Err(err) => Err(QueryError::new_err(err.to_string())), | ||
| } | ||
| Python::with_gil(|py| { | ||
| let taos = self.current_cursor()?; | ||
| let rs = py.allow_threads(|| { | ||
| taos.query_with_req_id(sql, req_id) | ||
| }).map_err(|err| QueryError::new_err(err.to_string()))?; | ||
| Ok(rs.affected_rows()) | ||
| }) | ||
| } |
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.
These four methods (query, query_with_req_id, execute, execute_with_req_id) contain duplicated logic for handling GIL release and error mapping. This can be refactored to improve maintainability by extracting the common patterns into helper methods.
You can introduce two private helper methods, query_wrapper and execute_wrapper, to the impl Connection block (around line 109) to encapsulate this logic:
fn query_wrapper<F>(&self, py: Python, query_fn: F) -> PyResult<TaosResult>
where
F: FnOnce() -> taos::Result<ResultSet> + Send,
{
let rs = py.allow_threads(query_fn)
.map_err(|err| QueryError::new_err(err.to_string()))?;
let cols = rs.num_of_fields();
Ok(TaosResult {
_inner: rs,
_block: None,
_current: 0,
_num_of_fields: cols as _,
_tz: self._tz,
})
}
fn execute_wrapper<F>(&self, py: Python, query_fn: F) -> PyResult<i32>
where
F: FnOnce() -> taos::Result<ResultSet> + Send,
{
let rs = py.allow_threads(query_fn)
.map_err(|err| QueryError::new_err(err.to_string()))?;
Ok(rs.affected_rows())
}With these helpers, the public methods can be simplified as shown in the suggestion below.
pub fn query(&self, sql: &str) -> PyResult<TaosResult> {
Python::with_gil(|py| {
let taos = self.current_cursor()?;
self.query_wrapper(py, || taos.query(sql))
})
}
pub fn query_with_req_id(&self, sql: &str, req_id: u64) -> PyResult<TaosResult> {
Python::with_gil(|py| {
let taos = self.current_cursor()?;
self.query_wrapper(py, || taos.query_with_req_id(sql, req_id))
})
}
pub fn execute(&self, sql: &str) -> PyResult<i32> {
Python::with_gil(|py| {
let taos = self.current_cursor()?;
self.execute_wrapper(py, || taos.query(sql))
})
}
pub fn execute_with_req_id(&self, sql: &str, req_id: u64) -> PyResult<i32> {
Python::with_gil(|py| {
let taos = self.current_cursor()?;
self.execute_wrapper(py, || taos.query_with_req_id(sql, req_id))
})
}
Description
I created this to address #371. I'm still working through testing it but it seems to compile and run. Open to any feedback
Jira: https://jira.taosdata.com:18080/browse/TD-
Checklist
Please check the items in the checklist if applicable.