diff --git a/common/params.cc b/common/params.cc index 3c5efd96d7ea73..5714619130f447 100644 --- a/common/params.cc +++ b/common/params.cc @@ -5,7 +5,6 @@ #include #include -#include #include #include "common/queue.h" @@ -15,11 +14,6 @@ namespace { -volatile sig_atomic_t params_do_exit = 0; -void params_sig_handler(int signal) { - params_do_exit = 1; -} - int fsync_dir(const std::string &path) { int result = -1; int fd = HANDLE_EINTR(open(path.c_str(), O_RDONLY, 0755)); @@ -282,27 +276,8 @@ int Params::remove(const std::string &key) { return fsync_dir(getParamPath()); } -std::string Params::get(const std::string &key, bool block) { - if (!block) { - return util::read_file(getParamPath(key)); - } else { - // blocking read until successful - params_do_exit = 0; - void (*prev_handler_sigint)(int) = std::signal(SIGINT, params_sig_handler); - void (*prev_handler_sigterm)(int) = std::signal(SIGTERM, params_sig_handler); - - std::string value; - while (!params_do_exit) { - if (value = util::read_file(getParamPath(key)); !value.empty()) { - break; - } - util::sleep_for(100); // 0.1 s - } - - std::signal(SIGINT, prev_handler_sigint); - std::signal(SIGTERM, prev_handler_sigterm); - return value; - } +std::string Params::get(const std::string &key) { + return util::read_file(getParamPath(key)); } std::map Params::readAll() { diff --git a/common/params.h b/common/params.h index d726a6185f3c77..075087aea5f970 100644 --- a/common/params.h +++ b/common/params.h @@ -39,9 +39,9 @@ class Params { void clearAll(ParamKeyType type); // helpers for reading values - std::string get(const std::string &key, bool block = false); - inline bool getBool(const std::string &key, bool block = false) { - return get(key, block) == "1"; + std::string get(const std::string &key); + inline bool getBool(const std::string &key) { + return get(key) == "1"; } std::map readAll(); diff --git a/common/params_pyx.pyx b/common/params_pyx.pyx index 2d11ab649d672e..ba6668d1c9fc65 100644 --- a/common/params_pyx.pyx +++ b/common/params_pyx.pyx @@ -1,5 +1,6 @@ # distutils: language = c++ # cython: language_level = 3 +import time from libcpp cimport bool from libcpp.string cimport string from libcpp.vector cimport vector @@ -15,8 +16,8 @@ cdef extern from "common/params.h": cdef cppclass c_Params "Params": c_Params(string) except + nogil - string get(string, bool) nogil - bool getBool(string, bool) nogil + string get(string) nogil + bool getBool(string) nogil int remove(string) nogil int put(string, string) nogil void putNonBlocking(string, string) nogil @@ -63,24 +64,22 @@ cdef class Params: cdef string k = self.check_key(key) cdef string val with nogil: - val = self.p.get(k, block) + val = self.p.get(k) - if val == b"": - if block: - # If we got no value while running in blocked mode - # it means we got an interrupt while waiting - raise KeyboardInterrupt - else: - return None + # If blocking is enabled, loop until a non-empty value is retrieved + while block and val == b"": + time.sleep(0.1) + with nogil: + val = self.p.get(k) + if val == b"": + return None return val if encoding is None else val.decode(encoding) def get_bool(self, key, bool block=False): cdef string k = self.check_key(key) - cdef bool r - with nogil: - r = self.p.getBool(k, block) - return r + val = self.get(k, block) + return True if val == b"1" else False def put(self, key, dat): """