diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9e2096c..9a5f9e2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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" @@ -72,7 +72,7 @@ jobs: # name: "amd64" # runs-on: "windows-cuda-12-0" - os: "windows" - name: "latest" + name: "" runs-on: "windows-latest" steps: @@ -114,8 +114,8 @@ 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/upload-release-asset@v1.0.1 if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') @@ -123,6 +123,6 @@ jobs: 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 diff --git a/.github/workflows/quality-gate.yml b/.github/workflows/quality-gate.yml index e9c7966..715b1da 100644 --- a/.github/workflows/quality-gate.yml +++ b/.github/workflows/quality-gate.yml @@ -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" @@ -28,7 +28,7 @@ jobs: # name: "amd64" # runs-on: "windows-cuda-12-0" - os: "windows" - name: "latest" + name: "" runs-on: "windows-latest" steps: @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ac5e97..3a9108f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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") diff --git a/Makefile b/Makefile index e470ae4..f0da019 100644 --- a/Makefile +++ b/Makefile @@ -35,12 +35,12 @@ 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: @@ -48,18 +48,18 @@ 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 diff --git a/README.md b/README.md index 46a6368..0a9faed 100644 --- a/README.md +++ b/README.md @@ -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 `` on Windows and `` on UNIX (Linux and MacOS). @@ -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 @@ -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 .. @@ -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. @@ -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 @@ -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 diff --git a/examples/server/server.cc b/examples/server/server.cc index 99d6fc6..65b206c 100644 --- a/examples/server/server.cc +++ b/examples/server/server.cc @@ -15,7 +15,7 @@ class Server { public: Server() { - dylib_ = std::make_unique("./engines/cortex.python-runtime", "engine"); + dylib_ = std::make_unique("./engines/cortex.python", "engine"); auto func = dylib_->get_function("get_engine"); engine_ = func(); } diff --git a/src/python_runtime_engine.cc b/src/python_engine.cc similarity index 81% rename from src/python_runtime_engine.cc rename to src/python_engine.cc index 5a9754e..aed22a3 100644 --- a/src/python_runtime_engine.cc +++ b/src/python_engine.cc @@ -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) @@ -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_body, std::function&& callback) { @@ -35,7 +35,7 @@ void PythonRuntimeEngine::HandlePythonFileExecutionRequest( } -void PythonRuntimeEngine::HandlePythonFileExecutionRequestImpl( +void PythonEngine::HandlePythonFileExecutionRequestImpl( PythonRuntime::PythonFileExecution::PythonFileExecutionRequest&& request, std::function && callback) { @@ -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; @@ -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 child_process_args; child_process_args.push_back(const_cast(child_process_exe_path.c_str())); child_process_args.push_back(const_cast("--run_python_file")); @@ -113,6 +113,6 @@ void PythonRuntimeEngine::HandlePythonFileExecutionRequestImpl( extern "C" { EngineI* get_engine() { - return new PythonRuntimeEngine(); + return new PythonEngine(); } } // extern C diff --git a/src/python_runtime_engine.h b/src/python_engine.h similarity index 90% rename from src/python_runtime_engine.h rename to src/python_engine.h index 7d61f37..3434482 100644 --- a/src/python_runtime_engine.h +++ b/src/python_engine.h @@ -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, diff --git a/src/python_runtime_utils.h b/src/python_utils.h similarity index 96% rename from src/python_runtime_utils.h rename to src/python_utils.h index 77c90eb..7986758 100644 --- a/src/python_runtime_utils.h +++ b/src/python_utils.h @@ -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 @@ -34,7 +34,7 @@ #include #endif -namespace PythonRuntimeUtils { +namespace python_utils { typedef long Py_ssize_t; typedef struct _object PyObject; @@ -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 == "") { @@ -229,4 +229,4 @@ inline void ExecutePythonFile(std::string binary_exec_path, std::string py_file_ } -} // namespace PythonRuntimeUtils +} // namespace python_utils