refactor: Manage all py plugins (md and exe) in one class

All calls to/from python are performed on one thread via a simple
message queue (necessary because of the global interpreter lock).
This commit is contained in:
John Colvin committed 2025-06-16 11:08:58 -04:00
1 parent c7db105533
commit 8c23665d65
29 files changed
+855 -602

No files matched your search

@@ -1,46 +1,18 @@
#include "python_interop/md/md_bridge_impl.h"
#include "python_interop/md/md_provider.h"
#include "python_interop/python_activator.h"
#include "spark/io/json_formatter.h"
#include "spark/logging/logging.h"
#include "spark/logging/trace_log.h"
#include "spark/utils/Delegate.h"
#include "spark/utils/stopwatch.h"
#include <pybind11/pybind11.h>
#include <thread>
using std::shared_ptr;
namespace py = pybind11;
namespace bts::md::py_interop {
using bts::core::Delegate;
using bts::io::json_formatter;
using bts::utils::Stopwatch;
using python_interop::PythonActivatorT;
using std::string;
using std::vector;
#define ASSERT_FN(test, fn, ...) \
bts_assert_msgf(test, "md_bridge::" fn ". " __VA_ARGS__)
#define LOG_FN(level, fn, ...) LOG(md, level, "md_bridge::" fn ". " __VA_ARGS__)
#define DIAG_FN(fn, ...) LOG_FN(bts::Diag, fn, __VA_ARGS__)
#define INFO_FN(fn, ...) LOG_FN(bts::Info, fn, __VA_ARGS__)
#define WARN_FN(fn, ...) LOG_FN(bts::Warn, fn, __VA_ARGS__)
#define ERROR_FN(fn, ...) LOG_FN(bts::Error, fn, __VA_ARGS__)
#define _LOG(level, ...) LOG(OrderMan, level, __VA_ARGS__)
#define INFO(...) _LOG(bts::Info, __VA_ARGS__)
#define ERROR(...) _LOG(bts::Error, __VA_ARGS__)
#define _LOG_TRACE(level, name) \
LOG_TRACE(md, level, std::string("md_bridge"), std::string(name))
#define INFO_TRACE(name) _LOG_TRACE(bts::Info, name)
#define WARN_TRACE(name) _LOG_TRACE(bts::Warn, name)
#define ERROR_TRACE(name) _LOG_TRACE(bts::Error, name)
#define DIAG_TRACE_PARAMS(name, params) \
LOG_TRACE_PARAMS( \
md, bts::Diag, std::string("md_bridge"), std::string(name), params)
@@ -49,13 +21,6 @@ using std::vector;
LOG_TRACE_PARAMS( \
md, bts::Info, std::string("md_bridge"), std::string(name), params)
#define WARN_TRACE_PARAMS(name, params) \
LOG_TRACE_PARAMS( \
md, bts::Warn, std::string("md_bridge"), std::string(name), params)
MdBridgeImpl::MdBridgeImpl(string target, vector<string> module_paths)
: target(std::move(target)), module_paths(std::move(module_paths)) {}
void MdBridgeImpl::on_securities(
const std::vector<std::shared_ptr<PySecurity>> &securities) {
INFO_TRACE_PARAMS("on_securities", securities.size());
@@ -63,7 +28,7 @@ void MdBridgeImpl::on_securities(
}
void MdBridgeImpl::on_books(const std::vector<std::shared_ptr<PyBook>> &books) {
DIAG_TRACE_PARAMS("on_books", json_formatter(books.size()));
INFO_TRACE_PARAMS("on_books", json_formatter(books.size()));
incoming_books.push_many(books.begin(), books.end());
}
@@ -103,26 +68,6 @@ bool MdBridgeImpl::pop_trade(shared_ptr<const PyTrade> &result) {
return false;
}
int MdBridgeImpl::run_py(volatile bool *keep_going) {
bts_assert(keep_going != nullptr);
PythonActivatorT<MdProvider> provider_activator{target, module_paths};
provider = provider_activator.get_target([&](MdProvider &pro) {
// This will start another thread in python, and call back into c++ from there.
auto sw = Stopwatch::start_new();
pro.start(this);
INFO_TRACE_PARAMS(
"run_py",
json_formatter(fmt::format(
"MdProvider.start took {:.2f}ms", sw.elapsed().total_millis())));
});
// Release gil to let python run
py::gil_scoped_release gil_rel;
while (*keep_going) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
MdBridgeImpl::~MdBridgeImpl() = default;
} // namespace bts::md::py_interop
@@ -1,15 +1,13 @@
#pragma once
#include "python_interop/md/md_bridge.h"
#include "python_interop/md/md_provider.h"
#include "spark/collections/concurrent_queue.h"
namespace bts::md::py_interop {
class MdBridgeImpl : public MdBridge {
public:
MdBridgeImpl(std::string target, std::vector<std::string> module_paths);
~MdBridgeImpl();
~MdBridgeImpl() override;
// Python interface
void on_securities(
@@ -21,16 +19,12 @@ class MdBridgeImpl : public MdBridge {
bool pop_security(std::shared_ptr<const PySecurity> &result);
bool pop_book(std::shared_ptr<const PyBook> &result);
bool pop_trade(std::shared_ptr<const PyTrade> &result);
int run_py(volatile bool *keep_going);
private:
std::string target;
std::vector<std::string> module_paths;
collections::ConcurrentQueue<std::shared_ptr<const PySecurity>>
incoming_securities;
collections::ConcurrentQueue<std::shared_ptr<const PyBook>> incoming_books;
collections::ConcurrentQueue<std::shared_ptr<const PyTrade>> incoming_trades;
MdProvider *provider;
};
} // namespace bts::md::py_interop