python调用C函数
python 与 c可以相互调用,在做后台服务时底层服务用C/C++编写,通过python调用C库可以极大的提高开发效率。
下面对几种调用方式举例说明
1 python通过指针传递浮点型数组给C函数
bird = cdll.LoadLibrary("./bird.so")
aList=[1.0, 1.1, 1.2, 1.3, 1.4, 1.5]
arrayMy = (c_float*len(aList))
a = arrayMy()
for i in range(0, len(a)):
a[i] = aList[i]
count = c_int(len(a))
bird.ptr_test(a, count)
这里注意调用C函数时传入的数组类型定义方法与初始值设定,ctypes模块定义了与C语言数据类型对应的python类型,调用C函数时要确保传入类型一致。
2 python通过指针传递字符串数组给C函数
info='Hello , glade to meet you!'
pInfo = create_string_buffer(info, len(info))
bird.buf_in_test(info, sizeof(pInfo))
注意create_string_buffer的作用是把python中的字符串类型转换为一个指针指向的字符串数组。
3 python接收C函数返回的字符串以及长度
bufLen = 100
c_pBuf = create_string_buffer('', bufLen)
c_bufLen = c_int(bufLen)
bird.buf_out_test(c_pBuf, byref(c_bufLen)) print c_bufLen.value
print string_at(c_pBuf)
注意byref是通过指针地址传入参数
4 C函数里分配内存传给python使用
ptr_char = pointer(c_char())
retlen = c_int(0)
bird.malloc_test(byref(ptr_char), byref(retlen)) print string_at(ptr_char)
print retlen.value bird.free_test(ptr_char)
注意python里定义一个字符指针的方法ptr_char = pointer(c_char()),定义一个整型变量的方法retlen = c_int(0) , byref(ptr_char)取指针的地址传入函数。
5 测试的python与C源码
#!/usr/bin/python
from ctypes import *
bird = cdll.LoadLibrary("./bird.so")
aList=[1.0, 1.1, 1.2, 1.3, 1.4, 1.5]
arrayMy = (c_float*len(aList))
a = arrayMy()
for i in range(0, len(a)):
a[i] = aList[i]
count = c_int(len(a))
bird.ptr_test(a, count)
info='Hello , glade to meet you!'
pInfo = create_string_buffer(info, len(info))
bird.buf_in_test(info, sizeof(pInfo))
bufLen = 100
c_pBuf = create_string_buffer('', bufLen)
c_bufLen = c_int(bufLen)
bird.buf_out_test(c_pBuf, byref(c_bufLen))
print c_bufLen.value
print string_at(c_pBuf)
ptr_char = pointer(c_char())
retlen = c_int(0)
bird.malloc_test(byref(ptr_char), byref(retlen))
print string_at(ptr_char)
print retlen.value
bird.free_test(ptr_char)
#include <stdio.h>
#include <string.h>
#include <stdlib.h> int ptr_test(float * ptr, int count)
{
if(ptr){
int i = ;
printf("\n");
for (i = ; i < count; ++i){
printf("%f | ", ptr[i]);
}
printf("\n");
}
return ;
} int buf_in_test(char*buf, int len)
{
if(buf){
int i = ;
printf("\n");
for (i = ; i < len; ++i){
printf("%c", buf[i]);
}
printf("\n");
}
return ;
} int buf_out_test(char*buf, int *len)
{
if(buf){
char *test_str = "hello world!";
memcpy(buf, test_str, strlen(test_str));
*len = strlen(test_str);
}
return ;
} int malloc_test(char **pBuf, int *len)
{
*pBuf = (char*)malloc();
printf("malloc add:%p\n", *pBuf);
*len = ;
memcpy(*pBuf, "hello", );
return ;
} int free_test(char *pBuf)
{
if (pBuf){
printf("free add:%p\n", pBuf);
free(pBuf); }
return ;
}
6 ctypes里定义的数据类型与C语言数据类型对应关系

python调用C函数的更多相关文章
- Python 调用C函数
/******************************************************************** * Python 调用C函数 * 说明: * Tony在处理 ...
- Linux解决Python调用Matlab函数无法导入matlab.engine问题及其他注意事项
问题描述 Linux系统,根据matlab官方文档说明,利用Matlab中的API来实现Python调用Matlab函数.具体方法见文档: https://ww2.mathworks.cn/help/ ...
- python调用模块&函数
一般模块是抽象的概念,按照功能划分模块,尽可能保证每个模块互相独立. 一般模块里有多个函数.当然,如果你愿意,也可以把一个几个模块写进一个大函数.对于python 模块,每个模块可以包含多个函数,但一 ...
- python调用php函数
由于php不支持多线程,所以想借助python搞一个.1.import subprocessimport time#Simple caller, disguard outputmethod=" ...
- 项目记录 -- python调用回调函数
C源文件: static int get_callback(zpool_handle_t *zhp, void *data) { zprop_get_cbdata_t *cbp = (zprop_ge ...
- python 调用c语言函数
虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了 具体流程: c编写相关函数 ,编译成库 然后在python中加载这些库,指定调用函数. ...
- 【Python】python 调用c语言函数
虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了具体流程:c编写相关函数 ,编译成库然后在python中加载这些库,指定调用函数.这些函数 ...
- python调用c的方法
虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了 具体流程: c编写相关函数 ,编译成库 然后在python中加载这些库,指定调用函数. ...
- 在windows下用python调用darknet的yolo接口
0,目标 本人计算机环境:windows7 64位,安装了vs2015专业版,python3.5.2,cygwin,opencv3.3,无gpu 希望实现用python调用yolo函数,实现物体检测. ...
随机推荐
- luoguP1886 滑动窗口 [单调队列]
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- [C++ Calculator 项目] 基础运算实现
Calculator V1.1 注:这是C++计算器项目第二部分-运算 [基于初始部分增改而得] 源文件已上传至github 主要问题: Ⅰ.运算实现的问题在于( ) + - * /的优先级的处理,以 ...
- .babelrc 文件
文件干啥用的 babel是降es6转义成浏览器能理解的es5语法. 如果项目中用了babel 转移,需要定义babel需要的插件和预设转码. babel 一般可以配合 webpack . browse ...
- python自动化运维:系统基础信息模块
p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 第一章: 首先介绍下系统性能信息模块:psutil psutil能够轻松实现获取系统 ...
- codeforces 475D. CGCDSSQ
D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes Given a sequence of int ...
- 表达式求值(二叉树方法/C++语言描述)(一)
使用二叉树对算数表达式(以下简称为表达式)进行求值,实质上是将表达式转换为二叉树,对其进行后序遍历,得到后缀表达式的同时可以求得表达式的值.转换和求值的过程也需要借助数据结构栈的帮助. 二叉树数据结构 ...
- HTML5基本知识点
一.什么是HTML HTML是超文本标签语言,即网页的源码.而浏览器就是翻译解释HTML源码的工具. 二.HTML的基本格式 <!DOCTYPE html>: ①文档类型声明:让浏览器按照 ...
- 4.npm模块安装和使用(axios异步请求,lodash工具库)
建立package.json npm init 下载包 npm install axios npm install lodash 下载包,并加到package里面 npm install axios ...
- hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- akoj-1280另类阶乘问题
另类阶乘问题 Time Limit:3000MS Memory Limit:65536K Total Submit:22 Accepted:20 Description 大家都知道阶乘这个概念,举个 ...