Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically spawn python child process from provider C++ code #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ cc_binary(
name = "provider",
srcs = ["main.cpp"],
defines = ["PROVIDER=1"] + local_defines,
deps = [
":src_files",
],
deps = [":src_files"],
data = ["sorting_python/sorting.py"]
)

cc_binary(
Expand All @@ -74,6 +73,11 @@ cc_binary(
deps = [":src_files"],
)

py_binary(
name = "sorting",
srcs = ["sorting_python/sorting.py"],
)

################################# tests BUILD target #################################

cc_test(
Expand Down
1 change: 1 addition & 0 deletions include/IPC/zmq_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ZMQReceiver {
public:
ZMQReceiver();
std::string receive();
unsigned int getPort() const;
};

#endif // _ZMQ_RECEIVER_H_
1 change: 1 addition & 0 deletions include/IPC/zmq_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ZMQSender {
public:
ZMQSender();
void send(const std::string& message);
unsigned int getPort() const;
};

#endif // _ZMQ_SENDER_H_
5 changes: 3 additions & 2 deletions sorting_python/sorting.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import zmq
import sys

# Set up the context and responder socket
port_rec = int(input("Enter the ZMQ sender port number: "))
port_send = int(input("Enter the ZMQ receiver port number: "))
port_rec = int(sys.argv[1])
port_send = int(sys.argv[2])

context = zmq.Context()
responder = context.socket(zmq.REP)
Expand Down
4 changes: 4 additions & 0 deletions src/IPC/zmq_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ std::string ZMQReceiver::receive() {

return zmq_msg.to_string();
}

unsigned int ZMQReceiver::getPort() const {
return port;
}
6 changes: 5 additions & 1 deletion src/IPC/zmq_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ void ZMQSender::send(const std::string& message) {

zmq::message_t reply;
socket.recv(reply, zmq::recv_flags::none);
}
}

unsigned int ZMQSender::getPort() const {
return port;
}
29 changes: 25 additions & 4 deletions src/Peers/provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../../include/utility.h"

#include <algorithm>
#include <boost/process.hpp>
#include <cstdio>
#include <cstdlib>
#include <iostream>
Expand Down Expand Up @@ -78,10 +79,30 @@ void Provider::listen() {
server->closeConn();
}

if (task->getLeaderUuid() == uuid) {
leaderHandleTaskRequest(requesterIpAddr);
} else {
followerHandleTaskRequest();
try {
// Spin up python script child process
std::string script_path = "sorting_python/sorting.py";
std::vector<std::string> args{
std::to_string(zmq_sender.getPort()),
std::to_string(zmq_receiver.getPort())};
boost::process::child python_script("/usr/bin/env", "python3", script_path, args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this dependent on where a user would have their local python installation? for me, when i run where python3, i see that my python installation is in /usr/bin/python3. that won't work with this setup then, right?


// Process P2P communication
if (task->getLeaderUuid() == uuid) {
leaderHandleTaskRequest(requesterIpAddr);
} else {
followerHandleTaskRequest();
}

// Wait for python script to terminate
python_script.wait();

int exit_code = python_script.exit_code();
if (exit_code != 0) {
std::cout << "Error: Python child process exited with code: " << exit_code << std::endl;
}
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
}
}
Expand Down