Skip to content

Commit

Permalink
Enable live stdout redirection to Python
Browse files Browse the repository at this point in the history
  • Loading branch information
mducle committed Apr 24, 2024
1 parent 90e3dc5 commit 56816f1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/libpymcr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ namespace libpymcr {
return nargout;
}

template <class T> T evalloop(matlab::cpplib::FutureResult<T> resAsync) {
template <class T> T matlab_env::evalloop(matlab::cpplib::FutureResult<T> resAsync) {
std::chrono::duration<int, std::milli> period(1);
std::future_status status = resAsync.wait_for(std::chrono::duration<int, std::milli>(1));
while (status != std::future_status::ready) {
status = resAsync.wait_for(period);
// Prints outputs and errors
if(_m_output.get()->in_avail() > 0) {
py::gil_scoped_acquire gil_acquire;
py::print(_m_output.get()->str(), py::arg("flush")=true);
py::gil_scoped_release gil_release;
_m_output.get()->str(std::basic_string<char16_t>());
}
}
return resAsync.get();
}
Expand Down Expand Up @@ -59,8 +66,8 @@ namespace libpymcr {
// Re-aquire the GIL
py::gil_scoped_acquire gil_acquire;
// Prints outputs and errors
if(_m_output.get()->in_avail() > 0) {
py::print(_m_output.get()->str(), py::arg("flush")=true); }
//if(_m_output.get()->in_avail() > 0) {
// py::print(_m_output.get()->str(), py::arg("flush")=true); }
if(_m_error.get()->in_avail() > 0) {
py::print(_m_error.get()->str(), py::arg("file")=py::module::import("sys").attr("stderr"), py::arg("flush")=true); }
// Converts outputs to Python types
Expand Down
1 change: 1 addition & 0 deletions src/libpymcr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace libpymcr {
std::shared_ptr<StreamBuffer> _m_error_buf = std::static_pointer_cast<StreamBuffer>(_m_error);
pymat_converter _converter;
size_t _parse_inputs(std::vector<matlab::data::Array>& m_args, py::args py_args, py::kwargs& py_kwargs);
template <class T> T evalloop(matlab::cpplib::FutureResult<T> resAsync);
public:
py::object feval(const std::u16string &funcname, py::args args, py::kwargs& kwargs);
py::object call(py::args args, py::kwargs& kwargs);
Expand Down

0 comments on commit 56816f1

Please sign in to comment.