Skip to content

Commit

Permalink
refactor: rename cortex.python (#11)
Browse files Browse the repository at this point in the history
* refactor: rename

* refactor: file names

* refactor: rename classes

* fix: CI names

---------

Co-authored-by: vansangpfiev <[email protected]>
  • Loading branch information
vansangpfiev and sangjanai authored May 21, 2024
1 parent d586b54 commit bcada59
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 63 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
# name: "ubuntu-18-04"
# runs-on: "ubuntu-18-04"
- os: "linux"
name: "latest"
name: ""
runs-on: "ubuntu-latest"
- os: "mac"
name: "arm64"
Expand All @@ -72,7 +72,7 @@ jobs:
# name: "amd64"
# runs-on: "windows-cuda-12-0"
- os: "windows"
name: "latest"
name: ""
runs-on: "windows-latest"

steps:
Expand Down Expand Up @@ -114,15 +114,15 @@ jobs:
- name: Upload Artifact
uses: actions/upload-artifact@v2
with:
name: cortex.python-runtime-${{ matrix.os }}-${{ matrix.name }}
path: ./cortex.python-runtime.tar.gz
name: cortex.python-${{ matrix.os }}-${{ matrix.name }}
path: ./cortex.python.tar.gz

- uses: actions/[email protected]
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ needs.create-draft-release.outputs.upload_url }}
asset_path: ./cortex.python-runtime.tar.gz
asset_name: cortex.python-runtime-${{ needs.create-draft-release.outputs.version }}-${{ matrix.os }}-${{ matrix.name }}.tar.gz
asset_path: ./cortex.python.tar.gz
asset_name: cortex.python-${{ needs.create-draft-release.outputs.version }}-${{ matrix.os }}-${{ matrix.name }}.tar.gz
asset_content_type: application/gzip
8 changes: 4 additions & 4 deletions .github/workflows/quality-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
# name: "ubuntu-18-04"
# runs-on: "ubuntu-18-04"
- os: "linux"
name: "latest"
name: ""
runs-on: "ubuntu-latest"
- os: "mac"
name: "arm64"
Expand All @@ -28,7 +28,7 @@ jobs:
# name: "amd64"
# runs-on: "windows-cuda-12-0"
- os: "windows"
name: "latest"
name: ""
runs-on: "windows-latest"

steps:
Expand Down Expand Up @@ -70,5 +70,5 @@ jobs:
- name: Upload Artifact
uses: actions/upload-artifact@v2
with:
name: cortex.python-runtime-${{ matrix.os }}-${{ matrix.name }}
path: ./cortex.python-runtime.tar.gz
name: cortex.python-${{ matrix.os }}-${{ matrix.name }}
path: ./cortex.python.tar.gz
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.5)
project(cortex.python-runtime C CXX)
project(cortex.python C CXX)
set(TARGET engine)

include(CheckIncludeFileCXX)
Expand Down Expand Up @@ -34,7 +34,7 @@ else()
endif()
# This is the critical line for installing another package

add_library(${TARGET} SHARED src/python_runtime_engine.cc)
add_library(${TARGET} SHARED src/python_engine.cc)

if(UNIX AND NOT APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
Expand Down
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@ endif

package:
ifeq ($(OS),Windows_NT)
@powershell -Command "mkdir -p cortex.python-runtime; cp build\Release\engine.dll cortex.python-runtime\; cp -r build\python cortex.python-runtime\; 7z a -ttar temp.tar cortex.python-runtime\*; 7z a -tgzip cortex.python-runtime.tar.gz temp.tar;"
@powershell -Command "mkdir -p cortex.python; cp build\Release\engine.dll cortex.python\; cp -r build\python cortex.python\; 7z a -ttar temp.tar cortex.python\*; 7z a -tgzip cortex.python.tar.gz temp.tar;"
else
@mkdir -p cortex.python-runtime && \
cp build/libengine.$(shell uname | tr '[:upper:]' '[:lower:]' | sed 's/darwin/dylib/;s/linux/so/') cortex.python-runtime && \
cp -r build/python cortex.python-runtime
tar -czvf cortex.python-runtime.tar.gz cortex.python-runtime
@mkdir -p cortex.python && \
cp build/libengine.$(shell uname | tr '[:upper:]' '[:lower:]' | sed 's/darwin/dylib/;s/linux/so/') cortex.python && \
cp -r build/python cortex.python
tar -czvf cortex.python.tar.gz cortex.python
endif

run-e2e-test:
ifeq ($(RUN_TESTS),false)
@echo "Skipping tests"
else
ifeq ($(OS),Windows_NT)
@powershell -Command "mkdir -p .\examples\server\build\Release\engines\cortex.python-runtime; cd examples\server\build\Release; cp ..\..\..\..\build\Release\engine.dll engines\cortex.python-runtime; ..\..\..\..\.github\scripts\e2e-test-server-windows.bat server.exe ..\..\..\..\$(PYTHON_FILE_EXECUTION_PATH);"
@powershell -Command "mkdir -p .\examples\server\build\Release\engines\cortex.python; cd examples\server\build\Release; cp ..\..\..\..\build\Release\engine.dll engines\cortex.python; ..\..\..\..\.github\scripts\e2e-test-server-windows.bat server.exe ..\..\..\..\$(PYTHON_FILE_EXECUTION_PATH);"
else
@mkdir -p examples/server/build/engines/cortex.python-runtime && \
@mkdir -p examples/server/build/engines/cortex.python && \
cd examples/server/build && \
cp ../../../build/libengine.$(shell uname | tr '[:upper:]' '[:lower:]' | sed 's/darwin/dylib/;s/linux/so/') engines/cortex.python-runtime/ && \
cp ../../../build/libengine.$(shell uname | tr '[:upper:]' '[:lower:]' | sed 's/darwin/dylib/;s/linux/so/') engines/cortex.python/ && \
chmod +x ../../../.github/scripts/e2e-test-server-linux-and-mac.sh && ../../../.github/scripts/e2e-test-server-linux-and-mac.sh ./server ../../../$(PYTHON_FILE_EXECUTION_PATH)
endif
endif

clean:
ifeq ($(OS),Windows_NT)
cmd /C "rmdir /S /Q build examples\\server\\build cortex.python-runtime cortex.python-runtime.tar.gz cortex.python-runtime.zip"
cmd /C "rmdir /S /Q build examples\\server\\build cortex.python cortex.python.tar.gz cortex.python.zip"
else
rm -rf build examples/server/build cortex.python-runtime cortex.python-runtime.tar.gz cortex.python-runtime.zip
rm -rf build examples/server/build cortex.python cortex.python.tar.gz cortex.python.zip
endif
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cortex.python-runtime
# cortex.python
**Python embedding on C++**

Each python execution request will create a new child process for python runtime by using `<process.h>` on Windows and `<spawn.h>` on UNIX (Linux and MacOS).
Expand All @@ -8,13 +8,13 @@ Each python execution request will create a new child process for python runtime
### Linux and MacOS
1. Install dependencies:
```bash
# In cortex.python-runtime/ root dir
# In cortex.python/ root dir
./install_deps.sh
```

2. Build the project
```bash
# In cortex.python-runtime/ root dir
# In cortex.python/ root dir
mkdir build && cd build
cmake ..
make -j32
Expand All @@ -23,14 +23,14 @@ make -j32
### Windows (testing)
1. Install dependencies
```powershell
# In cortex.python-runtime\ root dir
# In cortex.python\ root dir
cmake -S ./third-party -B ./build_deps/third-party
cmake --build ./build_deps/third-party --config Release
```

2. Build the project
```powershell
# In cortex.python-runtime\ root dir
# In cortex.python\ root dir
mkdir -p build
cd build
cmake ..
Expand All @@ -47,11 +47,11 @@ Go to the `.../build/` directory.

While running the python-runtime with no argument, it will use the default `.../build/python/` library
```
~/cortex.python-runtime/build$ ./python-runtime
~/cortex.python/build$ ./python-runtime
```
Or, you can use your desired Python3 version by adding a path to the directory that contain the `libpython***.so` or `libpython***.dylib` file.
```
~/cortex.python-runtime/build$ ./python-runtime {PATH_TO_DYNAMIC_LIB_DIR}
~/cortex.python/build$ ./python-runtime {PATH_TO_DYNAMIC_LIB_DIR}
```

Then input the path to your Python file that need to be executed. You can input **multiple times**, each input will create a new child process for Python runtime.
Expand All @@ -75,7 +75,7 @@ Then input the path to your Python file that need to be executed. You can input
Updating... 🙏
### Linux
```bash
# In cortex.python-runtime/ root dir
# In cortex.python/ root dir
cd ./build/python/
export PYTHONHOME=$(pwd)
./bin/python3 -m ensurepip
Expand Down Expand Up @@ -104,37 +104,37 @@ print("Hello from Cortex!")

```
# Default library
~/cortex.python-runtime/build$ ./python-runtime
~/cortex.python/build$ ./python-runtime
python-runtime started
/home/jan/cortex/main.py
Created child process for Python embedding
No specified Python library path, using default Python library in /home/jan/cortex.python-runtime/build/python/
Found dynamic library file /home/jan/cortex.python-runtime/build/python/libpython3.10.so.1.0
Successully loaded Python dynamic library from file: /home/jan/cortex.python-runtime/build/python/libpython3.10.so.1.0
No specified Python library path, using default Python library in /home/jan/cortex.python/build/python/
Found dynamic library file /home/jan/cortex.python/build/python/libpython3.10.so.1.0
Successully loaded Python dynamic library from file: /home/jan/cortex.python/build/python/libpython3.10.so.1.0
Trying to run Python file in path /home/jan/cortex/main.py
/home/jan/cortex.python-runtime/build/python/lib/python/site-packages/
/home/jan/cortex.python-runtime/build/python/lib/python/lib-dynload/
/home/jan/cortex.python-runtime/build/python/lib/python/
/home/jan/cortex.python/build/python/lib/python/site-packages/
/home/jan/cortex.python/build/python/lib/python/lib-dynload/
/home/jan/cortex.python/build/python/lib/python/
Hello from Cortex!
/home/jan/cortex/main.py
Created child process for Python embedding
No specified Python library path, using default Python library in /home/jan/cortex.python-runtime/build/python/
Found dynamic library file /home/jan/cortex.python-runtime/build/python/libpython3.10.so.1.0
Successully loaded Python dynamic library from file: /home/jan/cortex.python-runtime/build/python/libpython3.10.so.1.0
No specified Python library path, using default Python library in /home/jan/cortex.python/build/python/
Found dynamic library file /home/jan/cortex.python/build/python/libpython3.10.so.1.0
Successully loaded Python dynamic library from file: /home/jan/cortex.python/build/python/libpython3.10.so.1.0
Trying to run Python file in path /home/jan/cortex/main.py
/home/jan/cortex.python-runtime/build/python/lib/python/site-packages/
/home/jan/cortex.python-runtime/build/python/lib/python/lib-dynload/
/home/jan/cortex.python-runtime/build/python/lib/python/
/home/jan/cortex.python/build/python/lib/python/site-packages/
/home/jan/cortex.python/build/python/lib/python/lib-dynload/
/home/jan/cortex.python/build/python/lib/python/
Hello from Cortex!
```

```
# With specific python version
~/cortex.python-runtime/build$ ./python-runtime /usr/lib/python3.10/config-3.10-x86_64-linux-gnu/
~/cortex.python/build$ ./python-runtime /usr/lib/python3.10/config-3.10-x86_64-linux-gnu/
python-runtime started
/home/jan/cortex/main.py
Expand Down
2 changes: 1 addition & 1 deletion examples/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class Server {
public:
Server() {
dylib_ = std::make_unique<dylib>("./engines/cortex.python-runtime", "engine");
dylib_ = std::make_unique<dylib>("./engines/cortex.python", "engine");
auto func = dylib_->get_function<EngineI*()>("get_engine");
engine_ = func();
}
Expand Down
24 changes: 12 additions & 12 deletions src/python_runtime_engine.cc → src/python_engine.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "python_runtime_engine.h"
#include "python_runtime_utils.h"
#include "python_engine.h"
#include "python_utils.h"
#include "trantor/utils/Logger.h"

#if defined(_WIN32)
Expand All @@ -14,18 +14,18 @@ constexpr const int k200OK = 200;
constexpr const int k400BadRequest = 400;
constexpr const int k500InternalServerError = 500;

PythonRuntimeEngine::~PythonRuntimeEngine() {}
PythonEngine::~PythonEngine() {}

void PythonRuntimeEngine::ExecutePythonFile(
void PythonEngine::ExecutePythonFile(
std::string binary_execute_path,
std::string file_execution_path,
std::string python_library_path) {

std::string current_dir_path = PythonRuntimeUtils::GetDirectoryPathFromFilePath(binary_execute_path);
PythonRuntimeUtils::ExecutePythonFile(current_dir_path, file_execution_path, python_library_path);
std::string current_dir_path = python_utils::GetDirectoryPathFromFilePath(binary_execute_path);
python_utils::ExecutePythonFile(current_dir_path, file_execution_path, python_library_path);
}

void PythonRuntimeEngine::HandlePythonFileExecutionRequest(
void PythonEngine::HandlePythonFileExecutionRequest(
std::shared_ptr<Json::Value> json_body,
std::function<void(Json::Value&&, Json::Value&&)>&& callback) {

Expand All @@ -35,7 +35,7 @@ void PythonRuntimeEngine::HandlePythonFileExecutionRequest(
}


void PythonRuntimeEngine::HandlePythonFileExecutionRequestImpl(
void PythonEngine::HandlePythonFileExecutionRequestImpl(
PythonRuntime::PythonFileExecution::PythonFileExecutionRequest&& request,
std::function<void(Json::Value&&, Json::Value&&)> && callback) {

Expand All @@ -57,11 +57,11 @@ void PythonRuntimeEngine::HandlePythonFileExecutionRequestImpl(
status_resp["status_code"] = k200OK;

#if defined(_WIN32)
std::wstring exe_path = PythonRuntimeUtils::getCurrentExecutablePath();
std::wstring exe_path = python_utils::getCurrentExecutablePath();
std::string exe_args_string = " --run_python_file " + file_execution_path;
if (python_library_path != "")
exe_args_string += " " + python_library_path;
std::wstring pyArgs = exe_path + PythonRuntimeUtils::stringToWString(exe_args_string);
std::wstring pyArgs = exe_path + python_utils::stringToWString(exe_args_string);

STARTUPINFOW si;
PROCESS_INFORMATION pi;
Expand All @@ -79,7 +79,7 @@ void PythonRuntimeEngine::HandlePythonFileExecutionRequestImpl(
WaitForSingleObject(pi.hProcess, INFINITE);
}
#else
std::string child_process_exe_path = PythonRuntimeUtils::getCurrentExecutablePath();
std::string child_process_exe_path = python_utils::getCurrentExecutablePath();
std::vector<char*> child_process_args;
child_process_args.push_back(const_cast<char*>(child_process_exe_path.c_str()));
child_process_args.push_back(const_cast<char*>("--run_python_file"));
Expand Down Expand Up @@ -113,6 +113,6 @@ void PythonRuntimeEngine::HandlePythonFileExecutionRequestImpl(

extern "C" {
EngineI* get_engine() {
return new PythonRuntimeEngine();
return new PythonEngine();
}
} // extern C
4 changes: 2 additions & 2 deletions src/python_runtime_engine.h → src/python_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include "json/forwards.h"
#include "src/python_file_execution_request.h"

class PythonRuntimeEngine : public EngineI {
class PythonEngine : public EngineI {
public:
~PythonRuntimeEngine() final;
~PythonEngine() final;

void ExecutePythonFile(
std::string binary_exec_path,
Expand Down
8 changes: 4 additions & 4 deletions src/python_runtime_utils.h → src/python_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#if defined(_WIN32)
#define PY_DL HMODULE
#define PY_LOAD_LIB(path) LoadLibraryW(PythonRuntimeUtils::stringToWString(path).c_str());
#define PY_LOAD_LIB(path) LoadLibraryW(python_utils::stringToWString(path).c_str());
#define GET_PY_FUNC GetProcAddress
#define PY_FREE_LIB FreeLibrary
#else
Expand All @@ -34,7 +34,7 @@
#include <mach-o/dyld.h>
#endif

namespace PythonRuntimeUtils {
namespace python_utils {

typedef long Py_ssize_t;
typedef struct _object PyObject;
Expand Down Expand Up @@ -166,7 +166,7 @@ inline void ClearAndSetPythonSysPath(std::string default_py_lib_path, PY_DL py_d
inline void ExecutePythonFile(std::string binary_exec_path, std::string py_file_path ,std::string py_lib_path) {

signal(SIGINT, SignalHandler);
std::string binary_dir_path = PythonRuntimeUtils::GetDirectoryPathFromFilePath(binary_exec_path);
std::string binary_dir_path = python_utils::GetDirectoryPathFromFilePath(binary_exec_path);

bool is_default_python_lib = false;
if (py_lib_path == "") {
Expand Down Expand Up @@ -229,4 +229,4 @@ inline void ExecutePythonFile(std::string binary_exec_path, std::string py_file_
}


} // namespace PythonRuntimeUtils
} // namespace python_utils

0 comments on commit bcada59

Please sign in to comment.