Skip to content

Commit

Permalink
Move hook
Browse files Browse the repository at this point in the history
  • Loading branch information
IsabelParedes committed Mar 15, 2024
1 parent f1290dc commit ca0b1fd
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 45 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ set(XEUS_PYTHON_SRC
src/xdebugpy_client.cpp
src/xdisplay.cpp
src/xdisplay.hpp
src/xhook.cpp
src/xinput.cpp
src/xinput.hpp
src/xinspect.cpp
Expand All @@ -175,6 +176,7 @@ set(XEUS_PYTHON_HEADERS
include/xeus-python/xinterpreter_raw.hpp
include/xeus-python/xtraceback.hpp
include/xeus-python/xutils.hpp
include/xeus-python/xhook.hpp
)

set(XPYTHON_SRC
Expand Down
40 changes: 40 additions & 0 deletions include/xeus-python/xhook.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/***************************************************************************
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay, and *
* Wolf Vollprecht *
* Copyright (c) 2018, QuantStack
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XPYT_HOOK_HPP
#define XPYT_HOOK_HPP

#include "xeus-python/xeus_python_config.hpp"
#include "xeus-zmq/xhook_base.hpp"

#include "pybind11/embed.h"
#include "pybind11/pybind11.h"

namespace py = pybind11;

namespace xpyt
{
XEUS_PYTHON_API
class hook : public xeus::xhook_base
{
public:
hook() = default;

void pre_hook() override;
void post_hook() override;
void run(std::shared_ptr<uvw::loop> loop) override;

private:
py::gil_scoped_acquire* p_acquire{ nullptr};
};

}

#endif
48 changes: 3 additions & 45 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,10 @@
#include "xeus-python/xpaths.hpp"
#include "xeus-python/xeus_python_config.hpp"
#include "xeus-python/xutils.hpp"
#include "xeus-python/xhook.hpp"

namespace py = pybind11;

class py_hook : public xeus::xhook_base
{
public:
py_hook() = default;

void pre_hook() override
{
// p_release = new py::gil_scoped_release();
if (!p_acquire)
{
std::cout << "Acquiring GIL\n";
p_acquire = new py::gil_scoped_acquire();
}
}

void post_hook() override
{
delete p_acquire;
p_acquire = nullptr;
std::cout << "Posthook\n";

}

void run(std::shared_ptr<uvw::loop>) override
{
std::cout << "Overriden run\n";

py::gil_scoped_acquire acquire;
std::cout << "After acquire gil\n";
py::exec(R"(
import asyncio
loop = asyncio.get_event_loop()
print('got a loop', loop)
loop.run_forever()
)");
}

private:
py::gil_scoped_acquire* p_acquire{ nullptr };
};



int main(int argc, char* argv[])
{
if (xpyt::should_print_version(argc, argv))
Expand Down Expand Up @@ -216,14 +174,14 @@ int main(int argc, char* argv[])
nl::json debugger_config;
debugger_config["python"] = executable;

auto hook = std::make_unique<py_hook>();
auto py_hook = std::make_unique<xpyt::hook>();

std::cout << "Making a server\n";

auto make_xserver = [&](xeus::xcontext& context,
const xeus::xconfiguration& config,
nl::json::error_handler_t eh) {
return xeus::make_xserver_uv_shell_main(context, config, eh, loop_ptr, std::move(hook));
return xeus::make_xserver_uv_shell_main(context, config, eh, loop_ptr, std::move(py_hook));
};

std::cout << "Done with the lambda\n";
Expand Down
51 changes: 51 additions & 0 deletions src/xhook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/***************************************************************************
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay, and *
* Wolf Vollprecht *
* Copyright (c) 2018, QuantStack
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef UVW_AS_LIB
#define UVW_AS_LIB
#include <uvw.hpp>
#endif

#include <iostream> // REMOVE

#include "xeus-python/xhook.hpp"

#include "pybind11/embed.h"
#include "pybind11/pybind11.h"

namespace py = pybind11;

namespace xpyt
{
void hook::pre_hook()
{
if (!p_acquire)
{
std::cout << "Acquiring GIL" << std::endl;
p_acquire = new py::gil_scoped_acquire();
}
}

void hook::post_hook()
{
delete p_acquire;
p_acquire = nullptr;
std::cout << "Deleted GIL" << std::endl;
}

void hook::run(std::shared_ptr<uvw::loop> /* loop */)
{
std::cout << "Running loop" << std::endl;
py::gil_scoped_acquire acquire;
py::module asyncio = py::module::import("asyncio");
py::object loop = asyncio.attr("get_event_loop")();
loop.attr("run_forever")();
}
}

0 comments on commit ca0b1fd

Please sign in to comment.