Python手册说您可以在C和C++中为Python创建模块.使用C++时,您可以利用类和模板之类的东西吗?它不会与其他库和解释器产生不兼容性吗?
我使用的是Python 3.2和C++.
我需要提取当前存储在PyObject中的C类型.我已检查过文档并用谷歌搜索它,似乎没有其他人需要这样做.
所以我有一个PyObject,并试图提取C值.我有实际需要从对象中提取值的函数列表,但我首先需要知道的是存储在对象本身中以调用正确的函数.
以防这有助于理解这里是我正在尝试的一些示例代码.
//Variable is a custom variant type to allow for generic functionality.
Variable ExtractArgument( PyObject * arg )
{
Variable value;
PyObject* result;
//now here is the problem, I need to know which Python function to call in order to
//extract the correct type;
value = PyLong_FromLong( arg );
//or
value = PyFloat_FromDouble( arg )
//ect.
return value;
}
Run Code Online (Sandbox Code Playgroud)
希望我能看到类似这样的东西
Variable ExtractArgument( PyObject * arg )
{
Variable value;
PyObject* result;
//PyType is not the actual …Run Code Online (Sandbox Code Playgroud) 我有一个嵌入式Python程序,它在C中的一个线程中运行.
当Python解释器切换线程上下文(让控制到另一个线程)时,我希望得到通知,以便我可以执行某些必要的操作.
这似乎Py_AddPendingCall正是我正在寻找的.但是,API文档在这个函数上非常简短,我对应该如何Py_AddPendingCall使用感到困惑.通过阅读文档,我的理解是:
Py_AddPendingCall并分配处理函数.我用Google搜索了一些显示如何使用的代码Py_AddPendingCall,但我找不到任何东西.我自己尝试使用它根本行不通.永远不会调用处理程序.
我的工作线程代码:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
const char* PYTHON_CODE =
"while True:\n"
" for i in range(0,10): print(i)\n"
"\n";
int handler(void* arg)
{
printf("Pending Call invoked!\n");
abort();
}
void* worker_thread(void*)
{
PyGILState_STATE state = PyGILState_Ensure();
int res = Py_AddPendingCall(&func, nullptr);
cout << "Result: " << res << endl;
PyRun_SimpleString(CODE);
PyGILState_Release(state);
return 0;
}
int main()
{
Py_Initialize();
PyEval_InitThreads();
PyEval_ReleaseLock();
pthread_t threads[4];
for …Run Code Online (Sandbox Code Playgroud) 我应该打电话的时候有点困惑PyEval_InitThreads.一般来说,我知道PyEval_InitThreads只要使用非Python线程(即在扩展模块中生成的线程),就必须调用它.
但是,如果PyEval_InitThreads是嵌入Python解释器的C程序,或导入C扩展模块的Python程序,或者两者兼而有之,我很困惑.
因此,如果我编写一个内部启动线程的C扩展模块,PyEval_InitThreads在初始化模块时是否需要调用?
此外,PyEval_InitThreads 隐式获取全局解释器锁.因此,在调用之后PyEval_InitThreads,可能必须释放 GIL 或随后发生死锁.那么你如何释放锁?阅读文档后,PyEval_ReleaseLock()似乎是释放GIL的方法.但是,实际上,如果我在C扩展模块中使用以下代码:
PyEval_InitThreads();
PyEval_ReleaseLock();
Run Code Online (Sandbox Code Playgroud)
...然后在运行时Python中止:
Fatal Python error: drop_gil: GIL is not locked
Run Code Online (Sandbox Code Playgroud)
那么在获得GIL后如何释放GIL PyEval_InitThreads?
我想从C扩展中切换1D NumPy.我在C API中看到了各种辅助函数,用于创建新数组,重新整形,索引特定值等.但我没有看到任何类似PyArray_Slice1D(数组,开始,停止,步骤).这样的事情存在吗?
我已经在大型C ++应用程序中嵌入了Python3。Python提供了用于自定义数据处理的用户脚本功能。
问题:我有许多与Python交互的线程,但是我真的不知道如何使用GIL保护我的代码。到目前为止,使代码工作的唯一方法是使用boost::mutex。
这是一个非常简化的示例,重现了我的问题:
Init()以初始化Python(静态函数)。Pythonize()在Python上做一些工作。第一次锁定GIL时,线程B被阻塞。代码:
#include <iostream>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include "Python.h"
struct RTMaps_GILLock
{
RTMaps_GILLock()
{
std::cout << "Locking..." << std::endl;
m_state = PyGILState_Ensure();
}
~RTMaps_GILLock()
{
std::cout << "Unlocking..." << std::endl;
PyGILState_Release(m_state);
}
private:
PyGILState_STATE m_state;
};
#define GILLOCK RTMaps_GILLock lock;
class PythonEmbed
{
public:
static void Init()
{
Py_Initialize();
// EDIT : adding those two lines made my day :
PyEval_InitThreads(); // This acquires GIL
PyEval_SaveThread(); …Run Code Online (Sandbox Code Playgroud) 我有一个PyUnicode对象,我正在尝试将其转换回C字符串(char *)。
我尝试执行的方式似乎无效。这是我的代码:
PyObject * objectCompName = PyTuple_GET_ITEM(compTuple, (Py_ssize_t) 0);
PyObject * ooCompName = PyUnicode_AsASCIIString(objectCompName);
char * compName = PyBytes_AsString(ooCompName);
Py_DECREF(ooCompName);
Run Code Online (Sandbox Code Playgroud)
我还有另一种/更好的方法吗?
我无法使用Python C API打开numpy。我有以下代码
#include<Python.h>
int main()
{
Py_Initialize();
PyRun_SimpleString("import numpy");
PyObject* numpy = PyImport_ImportModule("numpy");
Py_Finalize();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该行PyRun_SimpleString("import numpy")打印到控制台:
追溯(最近一次通话最近):文件“ C:\ Users \ matt.heath \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ numpy__init __。py”中的行1,在第180行中来自。从numpy.lib的第13行,导入add_newdocs文件“ C:\ Users \ matt.heath \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ numpy \ add_newdocs.py” import add_newdoc文件“ C:\ Users \ matt.heath \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages …
我看到那INCLUDE条路是sysconfig.get_path('include')。
但我看不到有任何类似的价值LIB。
NumPy 直接像os.path.join(sys.prefix, "libs")Windows中那样对其进行硬编码,否则(在Windows中get_config_var('LIBDIR')没有记录和丢失)。
还有更多受支持的方法吗?
将Python嵌入应用程序中并编写扩展类型时,可以signature使用适当制作的.tp_doc字符串将a添加到方法中。
static PyMethodDef Answer_methods[] = {
{ "ultimate", (PyCFunction)Answer_ultimate, METH_VARARGS,
"ultimate(self, question='Life, the universe, everything!')\n"
"--\n"
"\n"
"Return the ultimate answer to the given question." },
{ NULL }
};
Run Code Online (Sandbox Code Playgroud)
当help(Answer)执行时,返回(缩写)以下内容:
class Answer(builtins.object)
|
| ultimate(self, question='Life, the universe, everything!')
| Return the ultimate answer to the given question.
Run Code Online (Sandbox Code Playgroud)
很好,但是我使用的是Python3.6,它支持注释。我想将问题注释为字符串,并返回一个int函数。我试过了:
static PyMethodDef Answer_methods[] = {
{ "ultimate", (PyCFunction)Answer_is_ultimate, METH_VARARGS,
"ultimate(self, question:str='Life, the universe, everything!') -> int\n"
"--\n"
"\n"
"Return the ultimate answer to the …Run Code Online (Sandbox Code Playgroud) python-c-api ×10
python ×9
c ×5
python-3.x ×4
c++ ×3
numpy ×2
cpython ×1
gil ×1
python-3.6 ×1
signature ×1