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;
17pub(crate) mod py_async;
18mod py_err;
19mod server;
20
21pub use client::client_sync::{Client, Table, View};
22pub use client::proxy_session::ProxySession;
23pub use perspective_client::ViewWindow;
24pub use perspective_client::config::ViewConfigUpdate;
25use py_err::PyPerspectiveError;
26use pyo3::prelude::*;
27use tracing_subscriber::layer::SubscriberExt;
28use tracing_subscriber::util::SubscriberInitExt;
29use tracing_subscriber::{EnvFilter, fmt};
30
31/// Create a tracing filter which mimics the default behavior of reading from
32/// env, customized to exclude timestamp.
33/// [`tracing` filter docs](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html#per-layer-filtering)
34fn init_tracing() {
35    let fmt_layer = fmt::layer().without_time().with_target(true);
36    let filter_layer = EnvFilter::try_from_default_env()
37        .or_else(|_| EnvFilter::try_new("info"))
38        .unwrap();
39
40    tracing_subscriber::registry()
41        .with(filter_layer)
42        .with(fmt_layer)
43        .init();
44}
45
46/// Returns the number of threads the internal threadpool will use.
47#[pyfunction]
48fn num_cpus() -> i32 {
49    perspective_server::num_cpus()
50}
51
52/// Set the number of threads the internal threadpool will use. Can also be set
53/// with `NUM_OMP_THREADS` environment variable.
54#[pyfunction]
55fn set_num_cpus(num_cpus: i32) {
56    perspective_server::set_num_cpus(num_cpus)
57}
58
59/// Perspective Python main module.
60#[pymodule]
61fn perspective(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
62    init_tracing();
63    m.add_class::<client::client_sync::Client>()?;
64    m.add_class::<server::Server>()?;
65    m.add_class::<server::AsyncServer>()?;
66    m.add_class::<server::PySession>()?;
67    m.add_class::<server::PyAsyncSession>()?;
68    m.add_class::<client::client_sync::Table>()?;
69    m.add_class::<client::client_sync::View>()?;
70    m.add_class::<client::client_async::AsyncClient>()?;
71    m.add_class::<client::client_async::AsyncTable>()?;
72    m.add_class::<client::client_async::AsyncView>()?;
73    m.add_class::<client::proxy_session::ProxySession>()?;
74    m.add("PerspectiveError", py.get_type::<PyPerspectiveError>())?;
75    m.add_function(wrap_pyfunction!(num_cpus, m)?)?;
76    m.add_function(wrap_pyfunction!(set_num_cpus, m)?)?;
77    Ok(())
78}