Skip to content

Commit

Permalink
add interface to set default eventloop
Browse files Browse the repository at this point in the history
  • Loading branch information
philoinovsky committed Aug 9, 2021
1 parent a9efac6 commit 29408b0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
9 changes: 8 additions & 1 deletion include/boost/python/eventloop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <unordered_map>
#include <boost/asio.hpp>
#include <boost/python.hpp>
#include <boost/mpl/vector.hpp>

namespace boost { namespace python { namespace asio {

Expand Down Expand Up @@ -159,7 +160,13 @@ class event_loop
static void _sock_accept(event_loop& loop, object fut, object sock);
};

}}} // namespace boost::python
void set_default_event_loop(const boost::asio::io_context::strand& strand);

extern "C"
{
PyObject* PyInit_boost_el();
}

}}} // namespace boost::python

# endif
51 changes: 51 additions & 0 deletions src/eventloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,56 @@ void event_loop::call_exception_handler(object context)
}
}

void set_default_event_loop(const boost::asio::io_context::strand& strand)
{
import("boost_el");
object asyncio = import("asyncio");
object abstract_policy = asyncio.attr("AbstractEventLoopPolicy");

dict method_dict;
std::shared_ptr<event_loop> p_loop = std::make_shared<event_loop>(strand);

method_dict["get_event_loop"] = make_function(
[p_loop] (object e) {return object(boost::ref(*p_loop));},
default_call_policies(),
boost::mpl::vector<object, object>()
);

object class_boost_policy = call<object>(
(PyObject*)&PyType_Type,
str("BoostEventLoopPolicy"),
boost::python::make_tuple(abstract_policy),
method_dict);

object boost_policy_instance = class_boost_policy.attr("__call__")();
asyncio.attr("set_event_loop_policy")(boost_policy_instance);
}

BOOST_PYTHON_MODULE(boost_el)
{
class_<event_loop, boost::noncopyable>("Eventloop", init<boost::asio::io_context::strand&>())
.def("call_soon", &event_loop::call_soon)
.def("call_soon_thread_safe", &event_loop::call_soon_thread_safe)
.def("call_later", &event_loop::call_later)
.def("call_at", &event_loop::call_at)
.def("time", &event_loop::time)
.def("add_reader", &event_loop::add_reader)
.def("remove_reader", &event_loop::remove_reader)
.def("add_writer", &event_loop::add_writer)
.def("remove_writer", &event_loop::remove_writer)
.def("sock_recv", &event_loop::sock_recv)
.def("sock_recv_into", &event_loop::sock_recv_into)
.def("sock_sendall", &event_loop::sock_sendall)
.def("sock_connect", &event_loop::sock_connect)
.def("sock_accept", &event_loop::sock_accept)
.def("sock_sendfile", &event_loop::sock_sendfile)
.def("start_tls", &event_loop::start_tls)
.def("getaddrinfo", &event_loop::getaddrinfo)
.def("getnameinfo", &event_loop::getnameinfo)
.def("set_exception_handler", &event_loop::set_exception_handler)
.def("get_exception_handler", &event_loop::get_exception_handler)
.def("default_exception_handler", &event_loop::default_exception_handler)
.def("call_exception_handler", &event_loop::call_exception_handler);
}

}}} // namespace boost::python

0 comments on commit 29408b0

Please sign in to comment.