Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shutdown method to python-sdk; replace shutdown behavior stop->sh… #114

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python-sdk/python/eppo_client/_eppo_client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class EppoClient:
def set_is_graceful_mode(self, is_graceful_mode: bool): ...
def is_initialized(self) -> bool: ...
def wait_for_initialization(self) -> None: ...
def shutdown(self) -> None: ...

class ContextAttributes:
def __new__(
Expand Down
41 changes: 21 additions & 20 deletions python-sdk/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use pyo3::{
exceptions::{PyRuntimeError, PyTypeError},
intern,
prelude::*,
types::{PyBool, PyFloat, PyInt, PySet, PyString},
PyTraverseError, PyVisit,
};
use std::{
collections::HashMap,
ops::Deref,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
Arc, Mutex,
},
time::Duration,
};

use pyo3::{
exceptions::{PyRuntimeError, PyTypeError},
intern,
prelude::*,
types::{PyBool, PyFloat, PyInt, PySet, PyString},
PyTraverseError, PyVisit,
};

use eppo_core::{
configuration_fetcher::ConfigurationFetcher,
configuration_store::ConfigurationStore,
Expand Down Expand Up @@ -142,7 +141,7 @@ impl EvaluationResult {
pub struct EppoClient {
configuration_store: Arc<ConfigurationStore>,
evaluator: Evaluator,
poller_thread: Option<PollerThread>,
poller_thread: Mutex<Option<PollerThread>>,
assignment_logger: Py<AssignmentLogger>,
is_graceful_mode: AtomicBool,
}
Expand Down Expand Up @@ -452,12 +451,14 @@ impl EppoClient {
///
/// This method releases GIL, so other Python thread can make progress.
fn wait_for_initialization(&self, py: Python) -> PyResult<()> {
if let Some(poller) = &self.poller_thread {
py.allow_threads(|| poller.wait_for_configuration())
.map_err(|err| PyRuntimeError::new_err(err.to_string()))
} else {
Err(PyRuntimeError::new_err("poller is disabled"))
if let Ok(guard) = self.poller_thread.lock() {
if let Some(poller) = guard.as_ref() {
return py
.allow_threads(|| poller.wait_for_configuration())
.map_err(|err| PyRuntimeError::new_err(err.to_string()));
}
}
Err(PyRuntimeError::new_err("poller is disabled"))
}

/// Returns a set of all flag keys that have been initialized.
Expand Down Expand Up @@ -593,7 +594,7 @@ impl EppoClient {
Ok(EppoClient {
configuration_store,
evaluator,
poller_thread,
poller_thread: Mutex::new(poller_thread),
assignment_logger: config
.assignment_logger
.as_ref()
Expand Down Expand Up @@ -688,10 +689,10 @@ impl EppoClient {
}

pub fn shutdown(&self) {
if let Some(poller) = &self.poller_thread {
// Using `.stop()` instead of `.shutdown()` here because we don't need to wait for the
// poller thread to exit.
poller.stop();
if let Ok(mut guard) = self.poller_thread.lock() {
if let Some(poller) = guard.take() {
let _ = poller.shutdown();
}
}
}
}
Expand Down
Loading