Skip to content

Conversation

@wkrettek
Copy link

@wkrettek wkrettek commented Nov 7, 2025

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.

  • Is the user manual updated?
  • Are the test cases passed and automated?
  • Is there no significant decrease in test coverage?

@gemini-code-assist
Copy link

Summary of Changes

Hello @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 taos-ws-py library by strategically releasing the Python Global Interpreter Lock (GIL) during various I/O-bound database operations. By wrapping potentially long-running calls like queries, executions, cursor creation, schemaless data insertion, and connection establishment with py.allow_threads(), the library ensures that other Python threads are not blocked. This leads to improved responsiveness and parallel execution in multi-threaded Python applications, directly addressing issue #371 by preventing blocking behavior during database interactions.

Highlights

  • GIL Release for Query Operations: The query and query_with_req_id methods now explicitly release the Python Global Interpreter Lock (GIL) using py.allow_threads() during the actual database query execution. This allows other Python threads to run concurrently while the Rust code is performing potentially long-running I/O operations.
  • GIL Release for Execute Operations: Similar to query operations, the execute and execute_with_req_id methods also release the GIL during their database execution phase, improving concurrency for these operations.
  • GIL Release for Cursor Creation: The cursor method, responsible for creating a new database cursor, now releases the GIL during the builder.build() call, which can involve network or resource-intensive setup.
  • GIL Release for Schemaless Data Insertion: The put method for inserting schemaless data now releases the GIL while performing the taos.put(&data) operation, preventing Python threads from being blocked during data writes.
  • GIL Release for Connection Establishment: The connect function, which establishes a new database connection, now releases the GIL during the builder.build() call, allowing other Python threads to proceed during connection setup.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines 122 to 176
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())
})
}

Choose a reason for hiding this comment

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

medium

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))
        })
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant