-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decode tile off the main thread (#40)
* Decode tile off the main thread * remove builder * pubcrate visibility
- Loading branch information
1 parent
937ec7b
commit ed67b77
Showing
15 changed files
with
178 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
from ._decoder import Decoder as Decoder | ||
from ._decoder import DecoderRegistry as DecoderRegistry | ||
from ._geo import GeoKeyDirectory as GeoKeyDirectory | ||
from ._ifd import ImageFileDirectory as ImageFileDirectory | ||
from ._thread_pool import ThreadPool as ThreadPool | ||
from ._tiff import TIFF as TIFF | ||
from ._tile import Tile as Tile |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class ThreadPool: | ||
def __init__(self, num_threads: int) -> None: ... |
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,12 @@ | ||
from collections.abc import Buffer | ||
|
||
from ._decoder import DecoderRegistry | ||
from ._thread_pool import ThreadPool | ||
|
||
class Tile: | ||
async def decode( | ||
self, | ||
*, | ||
decoder_registry: DecoderRegistry | None = None, | ||
pool: ThreadPool | None = None, | ||
) -> Buffer: ... |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::sync::Arc; | ||
|
||
use pyo3::exceptions::PyValueError; | ||
use pyo3::prelude::*; | ||
|
||
use pyo3::sync::GILOnceCell; | ||
use rayon::{ThreadPool, ThreadPoolBuilder}; | ||
|
||
static DEFAULT_POOL: GILOnceCell<Arc<ThreadPool>> = GILOnceCell::new(); | ||
|
||
pub fn get_default_pool(py: Python<'_>) -> PyResult<Arc<ThreadPool>> { | ||
let runtime = DEFAULT_POOL.get_or_try_init(py, || { | ||
let pool = ThreadPoolBuilder::new().build().map_err(|err| { | ||
PyValueError::new_err(format!("Could not create rayon threadpool. {}", err)) | ||
})?; | ||
Ok::<_, PyErr>(Arc::new(pool)) | ||
})?; | ||
Ok(runtime.clone()) | ||
} | ||
|
||
#[pyclass(name = "ThreadPool", frozen, module = "async_tiff")] | ||
pub(crate) struct PyThreadPool(Arc<ThreadPool>); | ||
|
||
#[pymethods] | ||
impl PyThreadPool { | ||
#[new] | ||
fn new(num_threads: usize) -> PyResult<Self> { | ||
let pool = ThreadPoolBuilder::new() | ||
.num_threads(num_threads) | ||
.build() | ||
.map_err(|err| { | ||
PyValueError::new_err(format!("Could not create rayon threadpool. {}", err)) | ||
})?; | ||
Ok(Self(Arc::new(pool))) | ||
} | ||
} | ||
|
||
impl PyThreadPool { | ||
pub(crate) fn inner(&self) -> &Arc<ThreadPool> { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl AsRef<ThreadPool> for PyThreadPool { | ||
fn as_ref(&self) -> &ThreadPool { | ||
&self.0 | ||
} | ||
} |
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,40 @@ | ||
use async_tiff::Tile; | ||
use pyo3::prelude::*; | ||
use pyo3_async_runtimes::tokio::future_into_py; | ||
use pyo3_bytes::PyBytes; | ||
use tokio_rayon::AsyncThreadPool; | ||
|
||
use crate::decoder::get_default_decoder_registry; | ||
use crate::thread_pool::{get_default_pool, PyThreadPool}; | ||
use crate::PyDecoderRegistry; | ||
|
||
#[pyclass(name = "Tile")] | ||
pub(crate) struct PyTile(Option<Tile>); | ||
|
||
#[pymethods] | ||
impl PyTile { | ||
#[pyo3(signature = (*, decoder_registry=None, pool=None))] | ||
fn decode_async( | ||
&mut self, | ||
py: Python, | ||
decoder_registry: Option<&PyDecoderRegistry>, | ||
pool: Option<&PyThreadPool>, | ||
) -> PyResult<PyObject> { | ||
let decoder_registry = decoder_registry | ||
.map(|r| r.inner().clone()) | ||
.unwrap_or_else(|| get_default_decoder_registry(py)); | ||
let pool = pool | ||
.map(|p| Ok(p.inner().clone())) | ||
.unwrap_or_else(|| get_default_pool(py))?; | ||
let tile = self.0.take().unwrap(); | ||
|
||
let result = future_into_py(py, async move { | ||
let decoded_bytes = pool | ||
.spawn_async(move || tile.decode(&decoder_registry)) | ||
.await | ||
.unwrap(); | ||
Ok(PyBytes::new(decoded_bytes)) | ||
})?; | ||
Ok(result.unbind()) | ||
} | ||
} |
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
Oops, something went wrong.