Skip to main content

perspective_python/
lib.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13#![doc = include_str!("../docs/lib.md")]
14#![warn(unstable_features)]
15
16mod client;
17mod migrate;
18pub(crate) mod py_async;
19mod py_err;
20mod server;
21
22pub use client::client_sync::{Client, Table, View};
23pub use client::proxy_session::ProxySession;
24pub use perspective_client::ViewWindow;
25pub use perspective_client::config::ViewConfigUpdate;
26use py_err::PyPerspectiveError;
27use pyo3::prelude::*;
28use tracing_subscriber::layer::SubscriberExt;
29use tracing_subscriber::util::SubscriberInitExt;
30use tracing_subscriber::{EnvFilter, fmt};
31
32/// Create a tracing filter which mimics the default behavior of reading from
33/// env, customized to exclude timestamp.
34/// [`tracing` filter docs](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html#per-layer-filtering)
35fn init_tracing() {
36    let fmt_layer = fmt::layer().without_time().with_target(true);
37    let filter_layer = EnvFilter::try_from_default_env()
38        .or_else(|_| EnvFilter::try_new("info"))
39        .unwrap();
40
41    tracing_subscriber::registry()
42        .with(filter_layer)
43        .with(fmt_layer)
44        .init();
45}
46
47/// Returns the number of threads the internal threadpool will use.
48#[pyfunction]
49fn num_cpus() -> i32 {
50    perspective_server::num_cpus()
51}
52
53/// Set the number of threads the internal threadpool will use. Can also be set
54/// with `NUM_OMP_THREADS` environment variable.
55#[pyfunction]
56fn set_num_cpus(num_cpus: i32) {
57    perspective_server::set_num_cpus(num_cpus)
58}
59
60/// Perspective Python main module.
61#[pymodule]
62fn perspective(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
63    init_tracing();
64    m.add_class::<client::client_sync::Client>()?;
65    m.add_class::<server::Server>()?;
66    m.add_class::<server::AsyncServer>()?;
67    m.add_class::<server::PySession>()?;
68    m.add_class::<server::PyAsyncSession>()?;
69    m.add_class::<client::client_sync::Table>()?;
70    m.add_class::<client::client_sync::View>()?;
71    m.add_class::<client::client_async::AsyncClient>()?;
72    m.add_class::<client::client_async::AsyncTable>()?;
73    m.add_class::<client::client_async::AsyncView>()?;
74    m.add_class::<client::proxy_session::ProxySession>()?;
75    m.add_class::<server::virtual_server_sync::PyVirtualServer>()?;
76    m.add_class::<server::virtual_server_sync::PyVirtualDataSlice>()?;
77    m.add_class::<server::generic_sql_model::PyGenericSQLVirtualServerModel>()?;
78    m.add("PerspectiveError", py.get_type::<PyPerspectiveError>())?;
79    m.add_function(wrap_pyfunction!(num_cpus, m)?)?;
80    m.add_function(wrap_pyfunction!(set_num_cpus, m)?)?;
81    m.add_function(wrap_pyfunction!(migrate::migrate, m)?)?;
82    Ok(())
83}