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函数的更多相关文章

  1. Python 调用C函数

    /******************************************************************** * Python 调用C函数 * 说明: * Tony在处理 ...

  2. Linux解决Python调用Matlab函数无法导入matlab.engine问题及其他注意事项

    问题描述 Linux系统,根据matlab官方文档说明,利用Matlab中的API来实现Python调用Matlab函数.具体方法见文档: https://ww2.mathworks.cn/help/ ...

  3. python调用模块&函数

    一般模块是抽象的概念,按照功能划分模块,尽可能保证每个模块互相独立. 一般模块里有多个函数.当然,如果你愿意,也可以把一个几个模块写进一个大函数.对于python 模块,每个模块可以包含多个函数,但一 ...

  4. python调用php函数

    由于php不支持多线程,所以想借助python搞一个.1.import subprocessimport time#Simple caller, disguard outputmethod=" ...

  5. 项目记录 -- python调用回调函数

    C源文件: static int get_callback(zpool_handle_t *zhp, void *data) { zprop_get_cbdata_t *cbp = (zprop_ge ...

  6. python 调用c语言函数

    虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了 具体流程: c编写相关函数 ,编译成库 然后在python中加载这些库,指定调用函数. ...

  7. 【Python】python 调用c语言函数

    虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了具体流程:c编写相关函数 ,编译成库然后在python中加载这些库,指定调用函数.这些函数 ...

  8. python调用c的方法

    虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了 具体流程: c编写相关函数 ,编译成库 然后在python中加载这些库,指定调用函数. ...

  9. 在windows下用python调用darknet的yolo接口

    0,目标 本人计算机环境:windows7 64位,安装了vs2015专业版,python3.5.2,cygwin,opencv3.3,无gpu 希望实现用python调用yolo函数,实现物体检测. ...

随机推荐

  1. Linux下protobuf的编译与安装【各种奇葩问题】

    1.下载源码 首先,从github上下载protobuf的源码,地址:https://github.com/google/protobuf,我选择下载2.5.0版本. 2.编译protobuf 2.1 ...

  2. HTML中重要的知识点,表单

    今天跟大家分享一下有关HTML中比较重要的一个知识点-表单: <form></form>表单 这是一个双标签,form表单有两个必须要有的属性,①action就是指表单传递到的 ...

  3. iOS开发实战-基于SpriteKit的FlappyBird小游戏

    写在前面 最近一直在忙自己的维P恩的事情 公司项目也是一团乱 于是...随手找了个游戏项目改了改就上线了,就当充数了. SpriteKit简介 SpriteKit是iOS 7之后苹果推出的2D游戏框架 ...

  4. 读书笔记_MVC__关于通过js构建ORM,实现Model层

    最近一直在学习MVC构建富应用的WEB程序,自己一直对MVC的设计模式理解的不是十分透彻,终于在研读了github上Spine的源码之后,对构建Model层有了一点自己的理解. 本文仅为个人理解,如有 ...

  5. 定时器(setTimeout)的秘密

    原文地址:→传送门 写在前面 setTimeout()是大家再熟悉不过的定时器,但平时对定时器的了解甚少,于是想看看setTimeout()的原理机制. setTimeout()基础 setTimeo ...

  6. HBase(0.96以上版本)过滤器Filter详解及实例代码

    说明: 本文参考官方Ref Guide,Developer API和众多博客,并结合实测代码编写,详细总结HBase的Filter功能,并附上每类Filter的相应代码实现. 本文尽量遵从Ref Gu ...

  7. web项目-easyui-datagrid使用汇总

    一,引言 工作的需要,做了3年的wpf--,又因为项目的需求,回归到web的开发. ■  3 years ago,vue是我三年前没用过的玩意儿. ■  3 years ago,bootstrap组件 ...

  8. Classy(排序)

    Description In his memoir So, Anyway. . ., comedian John Cleese writes of the class di erence betwee ...

  9. 认真地搞OI

    新博客的开头 OI生涯的开始 #include<cstdio> int main() { puts("Hello world!"); ; }

  10. OC-UICollectionView实现瀑布流

    UICollectionView实现瀑布流 在iOS中可以实现瀑布流的目前已知的有2种方案: 使用UIScrollView自己封装一套,这种方案是应用于iOS6之前的,因为iOS6才出来UIColle ...