Commit 679dd481 authored by Jørgen Lien Sellæg's avatar Jørgen Lien Sellæg
Browse files

refactor to use threads

parent 9cdde0b5
add_definitions(-DPYTHON_PATH="/home/jorgels/src/python-meetup/pybind/embedded_example")
find_package(Threads)
add_executable(embedded_example main.cpp)
target_link_libraries(embedded_example PRIVATE pybind11::embed)
target_link_libraries(embedded_example PRIVATE pybind11::embed ${CMAKE_THREAD_LIBS_INIT})
#include <iostream>
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <thread>
namespace py = pybind11;
......@@ -37,19 +38,21 @@ int main() {
auto &controller = controller::get();
std::thread t([]() {
py::module::import("stopscript");
});
t.detach();
std::cout << "starting process" << std::endl;
std::cout << "waiting for cancel signal.";
py::module module;
int i = 0;
while(!controller.stop) {
if(!module) {
module = py::module::import("stopscript");
}
if(i == 10) {
module.attr("stop")();
}
std::cout << "." << std::flush;
sleep(1);
i++;
}
if(t.joinable()) {
t.join();
}
}
from Api import Controller
from time import sleep
def stop():
c = Controller()
c.stop = True
sleep(5)
c = Controller()
c.stop = True
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment