是否可以与C++ iostream和python进行互操作?我正在使用boost-python并希望将包含istream和ostream的函数包装为参数.
我有一个动态链接到Python解释器的C++应用程序.我希望能够从特定目录导入python模块.我想修改我的进程的PYTHONPATH,以便sys.path将包含我添加到PYTHONPATH的路径.根据这个文档,这似乎是它的工作方式:
http://docs.python.org/c-api/intro.html#embedding-python
但是,当我从Python-land打印sys.path时,它具有PYTHONPATH的原始内容而不是我设置的内容.这是我正在做的一个例子(使用Boost.Python):
int main(int argc, char* argv[])
{
_putenv_s("PYTHONPATH", "C:\\source\\\\modules");
Py_Initialize();
object main = import("__main__");
object global = (main.attr("__dict__"));
exec("import sys\nprint sys.path"), global, global);
}
Run Code Online (Sandbox Code Playgroud)
PS - 我知道还有其他方法可以实现我的目标,但这不是我所要求的.我想知道为什么Py_Initialize()在设置sys.path时不使用PYTHONPATH的当前值.或许我误解了它应该如何运作?
我目前正在为Python开发一个基于C++的模块.我发现Boost :: Python对我想要完成的工作非常有效.但是,我现在遇到了由Boost :: Python生成的docstring的一些问题.给出以下Boost :: Python定义:
BOOST_PYTHON_MODULE(gcsmt)
{
class_<gcsmt::Units>("Units", "Sets the units used as input.", no_init)
.def("PrintSupported", &gcsmt::Units::printSupported, "Print out all supported units.")
.def("SetDefault", &gcsmt::Units::setDefaultUnit, "Sets the default unit to be used for inputs/outputs.")
.staticmethod("PrintSupported")
.staticmethod("SetDefault")
.def(self_ns::str(self_ns::self))
;
}
Run Code Online (Sandbox Code Playgroud)
如果我编译,在Python中加载我的模块,并获得gscmt.Units类的帮助,输出如下:
>>> help(gcsmt.Units)
Help on class Units in module gcsmt:
class Units(Boost.Python.instance)
| Sets the units used as input.
|
| Method resolution order:
| Units
| Boost.Python.instance
| __builtin__.object
|
| Methods defined here:
|
| __reduce__ = <unnamed …Run Code Online (Sandbox Code Playgroud) 我正在努力在我们的测试套件应用程序中嵌入Python.目的是使用Python运行多个测试脚本来收集数据并生成测试报告.一次测试运行的多个测试脚本可以创建可在下一个脚本中使用的全局变量和函数.
该应用程序还提供在嵌入式解释器中导入的扩展模块,用于与应用程序交换某些数据.
但是用户也可以进行多次测试运行.我不想在多次测试运行之间共享那些全局变量,导入和交换的数据.我必须确保我以真实状态重新启动以控制测试环境并获得相同的结果.
我该如何重新初始化翻译?
我使用了Py_Initialize()和Py_Finalize(),但在第二次运行时,我第二次将我提供给解释器的扩展模块初始化时获得异常.文档警告不要多次使用它.
使用子解释器似乎与扩展模块初始化具有相同的警告.
我怀疑我的扩展模块初始化时出了问题,但我担心第三方扩展模块会出现同样的问题.
也许可以通过在其自己的进程中启动解释器来使其工作,以确保释放所有内存.
顺便说一句,我正在使用boost-python,它也使用Py_Finalize警告AGAINST!
有什么建议吗?
谢谢
我试图使用boost.python将一段C++代码包装到python lib中,但是,我发现多个实例不能同时运行:
代码(C++):
class Foo{
public:
Foo(){}
void run(){
int seconds = 2;
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
};
BOOST_PYTHON_MODULE(run_test)
{
using namespace boost::python;
class_<Foo>("test", init<>())
.def("run", &Foo::run)
;
}
Run Code Online (Sandbox Code Playgroud)
这是使用CMake(CMake)编译的:
add_library(run_test SHARED run_test.cpp)
target_link_libraries(run_test boost_python python2.7)
Run Code Online (Sandbox Code Playgroud)
并使用以下代码(Python)进行测试:
class Dos(threading.Thread):
def run(self):
printl('performing DoS attack')
proc = test()
proc.run()
for i in range(5):
t = Dos()
t.start()
Run Code Online (Sandbox Code Playgroud)
输出表明代码以非常奇怪的方式并行化.每个线程应该只需要2秒钟,并且我的四核机器上应该同时运行4个线程:
[2011-11-04 13:57:01] performing DoS attack
[2011-11-04 13:57:01] performing DoS attack …Run Code Online (Sandbox Code Playgroud) 我想将对象的引用存储为weak_ptr.在纯C++中,以下工作原理:
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
using namespace std;
using namespace boost;
struct Empty
{
Empty(){}
};
struct Store
{
weak_ptr<Empty> value;
Store(){};
void setValue(shared_ptr<Empty> v) {
cout << "storing " << v << endl;
this->value = weak_ptr<Empty>(v);
shared_ptr<Empty> v_ok = this->value.lock();
if (v_ok) {
cout << "ok, v has been stored" << endl;
}
}
shared_ptr<Empty> getValue() {
shared_ptr<Empty> p = this->value.lock();
if (p) {
cout << "stored value : " << p << endl;
} else { …Run Code Online (Sandbox Code Playgroud) 我有几个模块为一些简单类型定义转换器(例如int的列表std::vector<int>); 它们是独立模块的一部分,但有时它们都用在一个脚本中,这导致了
RuntimeWarning: to-Python converter for std::vector<int, std::allocator<int> > already registered; second conversion method ignored.
Run Code Online (Sandbox Code Playgroud)
如何检查某种类型的转换器是否已定义并跳过第二次注册?
我正在尝试构建我的第一个Boost.Python示例.
#include <iostream>
#include <boost/python.hpp>
using namespace boost::python;
class Hello {
public:
std::string greet() {
std::cout << "Hello World" << std::endl;
}
};
BOOST_PYTHON_MODULE(hello)
{
class_<Hello>("Hello")
.def("greet", &Hello::greet);
}
int main() {
std::cout << "Boost.Python Test" << std::endl;
Hello hello;
hello.greet();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:缺少Python开发标题,正如@cdhowie所指出的那样.我找到并包含了所需的头文件.现在链接器抱怨:
10:43:58 **** Build of configuration BoostPythonTest-DPar for project BoostPythonTest
****
make all
Building file: ../src/BoostPythonTest.cpp
Invoking: GCC C++ Compiler
/usr/local/bin/g++-4.7 -I/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -I/usr/include -I/usr/local/Cellar/gcc/4.7.2/gcc/include/c++/4.7.2 -O0 -g3 -p -pg -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/BoostPythonTest.d" -MT"src/BoostPythonTest.d" …Run Code Online (Sandbox Code Playgroud) 我目前正在使用Boost.Python,并希望得到一些帮助来解决一个棘手的问题.
上下文
当C++方法/函数暴露给Python时,它需要释放GIL(全局解释器锁)以让其他线程使用解释器.这样,当python代码调用C++函数时,解释器可以被其他线程使用.现在,每个C++函数看起来像这样:
// module.cpp
int myfunction(std::string question)
{
ReleaseGIL unlockGIL;
return 42;
}
Run Code Online (Sandbox Code Playgroud)
为了传递它来提升python,我做:
// python_exposure.cpp
BOOST_PYTHON_MODULE(PythonModule)
{
def("myfunction", &myfunction);
}
Run Code Online (Sandbox Code Playgroud)
问题
这个方案工作正常,但它暗示这module.cpp取决于Boost.Python没有充分理由.理想情况下,只python_exposure.cpp应该依赖Boost.Python.
解?
我的想法是用Boost.Function这样包装函数调用:
// python_exposure.cpp
BOOST_PYTHON_MODULE(PythonModule)
{
def("myfunction", wrap(&myfunction));
}
Run Code Online (Sandbox Code Playgroud)
这里wrap将负责在通话期间解锁GIL myfunction.这种方法的问题是wrap需要具有相同的签名,myfunction这几乎意味着重新实现Boost.Function...
如果有人对此问题有任何建议,我将非常感激.
有人能告诉我,如果我做错了什么.
我在Windows 7上使用Visual Studio 2013,我希望能够设置一个简单的Boost.Python项目.我不知道我是否在构建提升或在项目中包含提升时出错了.
错误
当我尝试#include任何boost python模块时,例如#include <boost/python/module.hpp>我在Visual Studio中收到以下错误.
1>c:\boost_1_55_0\boost\python\detail\wrap_python.hpp(50): fatal error C1083: Cannot open include file: 'pyconfig.h': No such file or directory
Run Code Online (Sandbox Code Playgroud)
建造
我试图遵循这个SO线程中的指令,其中KTC解决了Python,以及来自Boost的这个Python howto,但由于这两个链接都有点过时,所以做的事情有所不同,并且一些步骤似乎在较新版本的Boost中有所改变,我不得不即兴发布一些指示.
这就是我做的.
C:\boost_1_55_0.cmd.exe导航到C:\boost_1_55_0.(我没有使用Developer Command Prompt for VS2013发现下\Microsoft Visual Studio 12.0\Common7\Tools\Shortcuts.这应该没有任何区别,是否应该?提升1.55的官方指南没有具体提及使用Command Prompt for VS2013.bootstrapcmd.project-config.jam(创建bootstrap)并添加了我的Python 3.4安装路径C:\Python34.我的.jam …boost-python ×10
c++ ×8
boost ×7
python ×7
converter ×1
embed ×1
function ×1
iostream ×1
python-c-api ×1