Skip to content

Commit

Permalink
text_markdown, text_plain: release the GIL
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Jul 26, 2024
1 parent f81c1c9 commit d0121b6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Response {
let raw_bytes = &self.content.bind(py).as_bytes();

// Release the GIL here because decoding can be CPU-intensive
let (decoded_str, detected_encoding_name) = py.allow_threads(move || {
let (decoded_str, detected_encoding_name) = py.allow_threads(|| {
let encoding_name_bytes = &encoding_name.as_bytes().to_vec();
let encoding = Encoding::for_label(encoding_name_bytes).ok_or_else(|| {
PyErr::new::<exceptions::PyValueError, _>(format!(
Expand Down Expand Up @@ -68,14 +68,16 @@ impl Response {
#[getter]
fn text_markdown(&mut self, py: Python) -> PyResult<String> {
let raw_bytes = self.content.bind(py).as_bytes();
let text = from_read(raw_bytes, usize::MAX);
let text = py.allow_threads(|| from_read(raw_bytes, usize::MAX));
Ok(text)
}

#[getter]
fn text_plain(&mut self, py: Python) -> PyResult<String> {
let raw_bytes = self.content.bind(py).as_bytes();
let text = from_read_with_decorator(raw_bytes, usize::MAX, TrivialDecorator::new());
let text = py.allow_threads(|| {
from_read_with_decorator(raw_bytes, usize::MAX, TrivialDecorator::new())
});
Ok(text)
}
}

0 comments on commit d0121b6

Please sign in to comment.