Skip to content

Commit

Permalink
python: Add async client, update pyo3
Browse files Browse the repository at this point in the history
* Support for running maturin commands with uv
  * set `PSP_UV=1` in environment
  * use `uv venv` to create venv, then activate it
  * `uv pip install -r **/requirements.txt` to install requirements
* Update pyo3 from 0.21 to 0.23
  * this was to support use of the `pyo3-async-runtimes` crate
* Build `abi3` .so when building with `maturin develop`
  * **Caveat**: after this change, old non-abi3 .so's still lingering in
    the rust/perspective-python directory may be loaded preferentially
    by Python when using an editable install.  It is best to delete
    those old .so's.
* Repurpose PyClient as AsyncClient and export bindings to Python
* Add async client pytests for both cpython and pyodide
  * Uses `pytest-asyncio` plugin
* Add python and pytest vscode settings
  * Supports running cpython pytest suite from vscode's Test Explorer

Signed-off-by: Tom Jakubowski <[email protected]>
  • Loading branch information
tomjakubowski committed Feb 21, 2025
1 parent 550d5ed commit 7974866
Show file tree
Hide file tree
Showing 28 changed files with 649 additions and 255 deletions.
3 changes: 3 additions & 0 deletions .devcontainer/postcreate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ sudo apt-get update
sudo apt-get install -y kitware-archive-keyring
sudo apt-get install -y cmake

# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh

# Repo setup
pnpm install
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ rust/target*
Vagrantfile
vcpkg
venv/
rust/perspective-python/perspective_python-*.data
rust/perspective-python/perspective_python-*.data
.pytest-cache/
4 changes: 4 additions & 0 deletions .vscode/settings.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"PSP_ENABLE_WASM": "1"
},
"python.formatting.provider": "black",
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": [
"rust/perspective-python/perspective/tests/"
],
"rust-analyzer.server.extraEnv": {
"PSP_ROOT_DIR": "../..",
"PSP_DISABLE_CPP": "1",
Expand Down
88 changes: 57 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions rust/perspective-python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ crate-type = ["cdylib"]
[build-dependencies]
cmake = "0.1.50"
num_cpus = "^1.16.0"
pyo3-build-config = "0.20.2"
pyo3-build-config = "0.22.6"
python-config-rs = "0.1.2"

[dependencies]
Expand All @@ -67,7 +67,22 @@ async-lock = "2.5.0"
pollster = "0.3.0"
extend = "1.1.2"
futures = "0.3.28"
pyo3 = { version = "0.21.2", features = ["extension-module", "serde"] }
pythonize = "0.21.1"
pyo3 = { version = "0.23.4", features = [
"experimental-async",
"extension-module",
"serde",
"py-clone",
] }
pythonize = "0.23.0"
tracing = { version = ">=0.1.36" }
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }

[target.'cfg(target_os="emscripten")'.dependencies]
pyo3-async-runtimes = { version = "0.23", features = ["attributes"] }

[target.'cfg(not(target_os="emscripten"))'.dependencies]
pyo3-async-runtimes = { version = "0.23", features = [
"attributes",
"tokio-runtime",
] }
tokio = { version = "1.43.0", features = ["full"] }
20 changes: 14 additions & 6 deletions rust/perspective-python/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import * as fs from "node:fs";
import sh from "../../tools/perspective-scripts/sh.mjs";
import * as url from "url";
import * as TOML from "@iarna/toml";
import * as toml from "smol-toml";
import * as tar from "tar";
import * as glob from "glob";
import * as path from "path";
Expand Down Expand Up @@ -114,10 +114,10 @@ if (build_wheel) {
if (build_sdist) {
// `maturin sdist` has some issues with Cargo workspaces, so we assemble the sdist by hand here
// Note that the resulting sdist is _not_ a Cargo workspace, it is rooted in this package.
const cargo_toml = fs.readFileSync("./Cargo.toml");
const pyproject_toml = fs.readFileSync("./pyproject.toml");
const cargo = TOML.parse(cargo_toml);
const pyproject = TOML.parse(pyproject_toml);
const cargo_toml = fs.readFileSync("./Cargo.toml").toString('utf-8');
const pyproject_toml = fs.readFileSync("./pyproject.toml").toString('utf-8');
const cargo = toml.parse(cargo_toml);
const pyproject = toml.parse(pyproject_toml);

const version = cargo["package"]["version"];
const data_dir = `perspective_python-${version}.data`;
Expand Down Expand Up @@ -153,8 +153,16 @@ if (build_sdist) {
);
}

if (process.env['PSP_UV'] === '1') {
flags += ' --uv'
}

if (!build_wheel && !build_sdist) {
cmd.sh(`maturin develop --features=external-docs ${flags} ${target}`);
const dev_features = [
"abi3",
"external-docs",
];
cmd.sh(`maturin develop --features=${dev_features.join(',')} ${flags} ${target}`);
}

if (!cmd.isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion rust/perspective-python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"devDependencies": {
"@finos/perspective-scripts": "workspace:*",
"@iarna/toml": "^1.0.0-rc.1",
"smol-toml": "^1.3.1",
"glob": "^11",
"cpy": "^9.0.1",
"tar": "^7.4.3"
Expand Down
2 changes: 2 additions & 0 deletions rust/perspective-python/perspective/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Client",
"PerspectiveError",
"ProxySession",
"AsyncClient",
]

import functools
Expand All @@ -26,6 +27,7 @@
PerspectiveError,
ProxySession,
PySyncServer as Server,
AsyncClient,
# NOTE: these are classes without constructors,
# so we import them just for type hinting
Table, # noqa: F401
Expand Down
Loading

0 comments on commit 7974866

Please sign in to comment.