我正在尝试构建一个算法交易程序.程序的执行流程如下:
Server sends data via websocket -> Python program receives it and sends it to C++ program -> C++ program processes the data and sends some data to Python code -> Python code sends packets to Server
Run Code Online (Sandbox Code Playgroud)
我不是用C++构建整个东西的原因是因为Broker的API只支持Python,如果我切换到Python,我就无法执行我希望执行的操作.
数据的频率每秒至少约为50kb(二进制和Json).到目前为止,我发现了以下替代方案:
用C++代码嵌入Python.这看起来很棒,但我不确定是否能够导入整个库并在C++中使用类/方法(代理的客户端).
通过发送数据包进行通信(延迟是这里的问题)
将接收到的数据放在SQL数据库中,每隔X ms让C++查询一次.(再次,延迟)
有没有更好的方法来做到这一点?
如果您正在使用CPython(python的最常见实现),那么您可以创建一个可用作python模块的动态库.有 Boost.Python
哪个可以用作:
#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
Run Code Online (Sandbox Code Playgroud)
> import hello_ext
> print hello_ext.greet()
hello, world
Run Code Online (Sandbox Code Playgroud)
要使用python 3.7和boost 1.68.0进行构建,您可以使用以下CMake文件
cmake_minimum_required(VERSION 3.9.0 FATAL_ERROR)
project("boost_python_sample" LANGUAGES CXX)
set(BOOST_ROOT "C:/local/boost_1_68_0")
find_package(Boost REQUIRED COMPONENTS python37)
set(Python3_ROOT_DIR "C:/python37")
find_package(Python3 REQUIRED COMPONENTS Development)
add_library("boost_python_sample" SHARED "main.cpp")
target_link_libraries("boost_python_sample" Boost::python37 Python3::Python)
target_compile_definitions("boost_python_sample" PUBLIC "BOOST_PYTHON_STATIC_LIB")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
922 次 |
| 最近记录: |