SlideShare a Scribd company logo
1 of 23
Download to read offline
Cython的一点使用经验  
	
  
赖勇浩@齐昌网络	
  
2015.9.13.  
内容提要  
•  自我介绍	
  
•  Cython是什么?	
  
•  Cython有什么能力?	
  
•  一点使用经验	
  
•  Q&A	
  
2  
自我介绍  
•  赖勇浩,Python  的重度用户,PyCon  的老朋友。	
  
•  从业10年,创立广州齐昌网络科技有限公司,主
营技术咨询与开发外包。	
  
•  合著有《编写高质量代码:改善Python程序的91
个建议》一书。  
3  
Cython是什么?  
•  Cython	
  is	
  an	
  optimising	
  static	
  
compiler	
  for	
  both	
  the	
  Python	
  
programming	
  language	
  and	
  the	
  
extended	
  Cython	
  programming	
  
language	
  (based	
  on	
  Pyrex).	
    
4  
Cython是什么?  
•  It	
  makes	
  writing	
  C	
  extensions	
  for	
  
Python	
  as	
  easy	
  as	
  Python	
  itself.	
  	
  
– Cython	
  can	
  compile	
  (most)	
  regular	
  
Python	
  code	
  
– It	
  aims	
  to	
  become	
  a	
  superset	
  of	
  the	
  
[Python]	
  language	
  which	
  gives	
  it	
  
high-­‐level,	
  object-­‐oriented,	
  
functional,	
  and	
  dynamic	
  programming.  
5  
Cython有什么能力?  
•  无痛提升运行效率。	
  
•  Python程序防逆向工程。	
  
•  一点工作,数(百)倍运行效率。	
  
•  把好用的Python库给C/C++用。	
  
•  更方便、更简单地编写Python扩展。	
  
•  封装已有的C/C++库。	
  
6  
无痛提升运行效率。  
def	
  f(x):	
  
	
  	
  	
  	
  return	
  x**2-­‐x	
  
	
  
def	
  integrate_f(a,b,N):	
  
	
  	
  	
  	
  s	
  =	
  0	
  
	
  	
  	
  	
  dx	
  =	
  (b-­‐a)/N	
  
	
  	
  	
  	
  for	
  i	
  in	
  range(N):	
  
	
  	
  	
  	
  	
  	
  	
  	
  s	
  +=	
  f(a+i*dx)	
  
	
  	
  	
  	
  return	
  s	
  *	
  dx  
cython	
  ex1.py	
  
	
  
gcc	
  -­‐fPIC	
  -­‐shared	
  -­‐I/
usr/include/python2.7	
  
ex1.c	
  -­‐o	
  ex1.so	
  
7  
Python程序防逆向工程。  
没有字节码,顺手防逆向。
8  
一点工作,数(百)倍运行效率。  
def	
  f(double	
  x):	
  
	
  	
  	
  	
  return	
  x**2-­‐x	
  
	
  
def	
  integrate_f(double	
  a,	
  double	
  b,	
  int	
  N):	
  
	
  	
  	
  	
  cdef	
  int	
  i	
  
	
  	
  	
  	
  cdef	
  double	
  s,	
  dx	
  
	
  	
  	
  	
  s	
  =	
  0	
  
	
  	
  	
  	
  dx	
  =	
  (b-­‐a)/N	
  
	
  	
  	
  	
  for	
  i	
  in	
  range(N):	
  
	
  	
  	
  	
  	
  	
  	
  	
  s	
  +=	
  f(a+i*dx)	
  
	
  	
  	
  	
  return	
  s	
  *	
  dx  
9  
一点工作,数(百)倍运行效率。  
def	
  f(double	
  x):	
  
	
  	
  	
  	
  return	
  x**2-­‐x	
  
	
  
cdef	
  double	
  f(double	
  x)	
  except?	
  -­‐2:	
  
	
  	
  	
  	
  return	
  x**2-­‐x	
  
10  
把好用的Python库给C/C++用。  
#	
  pyurllib.pyx	
  
	
  
import	
  urllib	
  
	
  
cdef	
  public	
  char	
  *	
  urlopen(char	
  *	
  
url):	
  
	
  	
  	
  	
  content=	
  
	
  	
  	
  	
  	
  	
  	
  	
  urllib.urlopen(url).read()	
  
	
  	
  	
  	
  return	
  content	
  
   11  
把好用的Python库给C/C++用。  
#include	
  <Python.h>	
  
#include	
  "pyurllib.h"	
  
	
  
int	
  main	
  (int	
  argc,	
  char	
  **	
  
argv)	
  
{	
  
	
  	
  /*	
  Boiler	
  plate	
  init	
  Python	
  
*/	
  
	
  	
  Py_SetProgramName	
  (argv	
  
[0]);	
  
	
  	
  Py_Initialize	
  ();	
  
	
  
	
  	
  /*	
  Init	
  our	
  url	
  module	
  into	
  
Python	
  memory	
  */	
  
	
  	
  initpyurllib();	
  
	
  
	
  if	
  (argc	
  >=	
  2)	
  
	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  /*	
  call	
  directly	
  into	
  
our	
  cython	
  module	
  */	
  
	
  	
  	
  	
  	
  	
  	
  
printf("%s",urlopen(argv[1]));	
  
	
  	
  	
  	
  }	
  
	
  	
  else	
  
	
  	
  	
  	
  printf	
  ("require	
  url...
n");	
  
	
  
	
  	
  /*	
  cleanup	
  python	
  before	
  
exit	
  ...	
  */	
  
	
  	
  Py_Finalize	
  ();	
  
	
  
	
  	
  return	
  0;	
  
}  
12  
更方便、更简单地编写Python扩展。  
//	
  fic.c	
  
#include	
  “Python.h”	
  
static	
  PyObject*	
  fib(...)	
  {	
  
//	
  解释参数	
  
//	
  C	
  类型的实参转变为Python对象	
  
//  业务逻辑代码	
  
//	
  构建返回值的Python对象	
  
	
  	
  	
  	
  return	
  ret;	
  
}	
  
static	
  PyMethodDef	
  fib_methods[]	
  =	
  {	
  
	
  	
  	
  	
  {“fib”,	
  fib,	
  ...},	
  
	
  	
  	
  	
  {NULL,NULL,0,NULL},	
  
};	
  
void	
  initfib(void)	
  {	
  
	
  	
  	
  	
  (void)	
  Py_InitModule(...);	
  
}	
  
#setup.py	
  
module=Extension(‘fib’,	
  
sources=[‘fib.c’])	
  
setup(name=‘fib’,	
  	
  version=‘1.0,	
  
ext_modules=[module])	
  
	
  
$	
  python	
  setup.py	
  build_ext	
  –inplace	
  
	
  
>>>	
  import	
  fib	
  
>>>	
  fib.fib(2000)	
  
1	
  1	
  2	
  3	
  5	
  8	
  13	
  21	
  34	
  55	
  89	
  144	
  233	
  
377	
  610	
  987	
  1597  
  
13  
更方便、更简单地编写Python扩展。  
#	
  fib.pyx	
  
def	
  fib(n):	
  
	
  	
  	
  	
  a,	
  b	
  =	
  0,	
  1	
  
	
  	
  	
  	
  while	
  b	
  <	
  n:	
  
	
  	
  	
  	
  	
  	
  	
  	
  print	
  b,	
  
	
  	
  	
  	
  	
  	
  	
  	
  a,	
  b	
  =	
  b,	
  a	
  +	
  b	
  
	
  
from	
  distutils.core	
  import	
  
setup	
  
from	
  Cython.Build	
  import	
  
cythonize	
  
	
  
setup(	
  
	
  	
  	
  	
  
ext_modules=cythonize("fib.pyx
"),	
  
)	
  
$	
  python	
  setup.py	
  build_ext	
  –
inplace	
  
	
  
>>>	
  import	
  fib	
  
>>>	
  fib.fib(2000)	
  
1	
  1	
  2	
  3	
  5	
  8	
  13	
  21	
  34	
  55	
  89	
  144	
  
233	
  377	
  610	
  987	
  1597  
  
14  
封装已有的C/C++库。  
•  选择:ctypes/CFFI/boost.python/
SWIG/cython	
  
•  优势:	
  
– 易学,Python+C=Cython,重用旧知识	
  
– 好用,pxd,重用声明文件	
  
– C++支持全面,可从C++中回调Python函数,
为Python	
  class重载C++	
  class行为提供可能	
  
– ……  
15  
一点使用经验  
•  经历了一个项目:封装C&C++编写的数据库
驱动为Python模块。	
  
– 导出类型定义、宏与结构体	
  
– 导出函数、函数类型与复杂的类	
  
– 在Python类中使用C++类成员变量	
  
– 错误码与异常的转换	
  
– 编写一层C++代码简化接口  
16  
导出类型定义、宏与结构体  
cdef	
  extern	
  from	
  "ossTypes.h":	
  
	
  	
  	
  	
  ctypedef	
  char	
  CHAR	
  
	
  
	
  	
  	
  	
  ctypedef	
  unsigned	
  long	
  
long	
  UINT64	
  
	
  	
  	
  	
  ctypedef	
  long	
  long	
  SINT64	
  
	
  	
  	
  	
  ctypedef	
  long	
  long	
  INT64	
  
	
  
cdef	
  extern	
  from	
  "ossErr.h":	
  
	
  	
  	
  	
  enum:SDB_OK	
  
	
  	
  	
  	
  enum:SDB_DMS_EOC	
  
	
  
cdef	
  extern	
  from	
  "msg.h":	
  
	
  	
  	
  	
  ctypedef	
  struct	
  
MsgSysInfoRequest:	
  
	
  	
  	
  	
  	
  	
  	
  	
  pass	
  
	
  	
  	
  	
  ctypedef	
  struct	
  
MsgSysInfoReply:	
  
	
  	
  	
  	
  	
  	
  	
  	
  pass	
  
	
  	
  	
  	
  ctypedef	
  struct	
  
MsgOpReply:	
  
	
  	
  	
  	
  	
  	
  	
  	
  INT32	
  field_u_need	
  
  
17  
导出函数、函数类型与复杂的类  
cdef	
  extern	
  from	
  "_sdbapi.h"	
  namespace	
  "qc":	
  
	
  	
  	
  	
  SINT32	
  steal_size(const	
  CHAR*)	
  
	
  	
  	
  	
  ctypedef	
  void	
  (*network_func)(CHAR*	
  buff,	
  size_t	
  
size)	
  
	
  
	
  	
  	
  	
  cdef	
  cppclass	
  ConnectionContext:	
  
	
  	
  	
  	
  	
  	
  	
  	
  ConnectionContext()	
  except	
  +	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  CHAR*	
  send_buff	
  
	
  	
  	
  	
  	
  	
  	
  	
  INT32	
  send_buff_size	
  
	
  	
  	
  	
  	
  	
  	
  	
  network_func	
  sender	
  
	
  	
  	
  	
  	
  	
  	
  	
  network_func	
  recver	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  string	
  str()	
  
18  
在Python类中使用C++类成员变量  
import	
  socket	
  
cdef	
  class	
  Connection(object):	
  
	
  	
  	
  	
  cdef	
  ConnectionContext*	
  _ctx	
  
	
  	
  	
  	
  cdef	
  object	
  _sock	
  
	
  	
  	
  	
  def	
  __cinit__(self):	
  
	
  	
  	
  	
  	
  	
  	
  	
  self._ctx	
  =	
  new	
  ConnectionContext()	
  
	
  	
  	
  	
  	
  	
  	
  	
  self._sock	
  =	
  socket.socket()	
  
	
  	
  	
  	
  def	
  __dealloc__(self):	
  
	
  	
  	
  	
  	
  	
  	
  	
  del	
  self._ctx  
19  
错误码与异常的转换  
•  需要有统一的错误处理机制,C/C++  的错
误码方式需要转换为Python异常。  
class	
  Error(StandardError):	
  
	
  	
  	
  	
  def	
  __init__(self,	
  msg,	
  err=None):	
  
	
  	
  	
  	
  	
  	
  	
  	
  self.err	
  =	
  err	
  
	
  	
  	
  	
  	
  	
  	
  	
  self.msg	
  =	
  msg	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  def	
  __str__(self):	
  
	
  	
  	
  	
  	
  	
  	
  	
  return	
  'Error(err=%s,	
  msg="%s")'	
  %	
  
(self.err,	
  self.msg)	
  
	
   20  
编写一层C++代码简化接口  
•  避免需要把复杂的类用Cython声明一遍	
  
•  避免C++代码风格传导到Python	
  
•  更一致的生命周期管理	
  
•  编译器、平台相关的需求(如#pragma	
  
pack)	
  
•  胶合层提供更高的灵活性	
  
21  
关于齐昌  
客户   技术  
22  
Q&A  
23  

More Related Content

What's hot

Groovy:Candy for Java Developers
Groovy:Candy for Java DevelopersGroovy:Candy for Java Developers
Groovy:Candy for Java Developersfoxgem
 
Python匯出入csv以及繪製圖表初稿
Python匯出入csv以及繪製圖表初稿Python匯出入csv以及繪製圖表初稿
Python匯出入csv以及繪製圖表初稿jiannrong
 
全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版Simen Li
 
Introduction To Direct Show
Introduction To Direct ShowIntroduction To Direct Show
Introduction To Direct Showguestce3937
 
3 Python开发风格与建议
3 Python开发风格与建议3 Python开发风格与建议
3 Python开发风格与建议March Liu
 
Ptyhon 教學 003 函數
Ptyhon 教學 003 函數Ptyhon 教學 003 函數
Ptyhon 教學 003 函數信宏 陳
 
Golang server design pattern
Golang server design patternGolang server design pattern
Golang server design pattern理 傅
 
嵌入式平台移植技巧概說
嵌入式平台移植技巧概說嵌入式平台移植技巧概說
嵌入式平台移植技巧概說Joseph Lu
 
Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command lineWen Liao
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享Chong-Kuan Chen
 
Light talk @ coscup 2011 : Incremental Global Prelink for Android
Light talk @ coscup 2011 : Incremental Global Prelink for AndroidLight talk @ coscup 2011 : Incremental Global Prelink for Android
Light talk @ coscup 2011 : Incremental Global Prelink for AndroidKito Cheng
 
少年科技人雜誌 2015 年八月
少年科技人雜誌 2015 年八月少年科技人雜誌 2015 年八月
少年科技人雜誌 2015 年八月鍾誠 陳鍾誠
 
Hcsm lect-20120913
Hcsm lect-20120913Hcsm lect-20120913
Hcsm lect-20120913lusecheng
 
Golangintro
GolangintroGolangintro
Golangintro理 傅
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析wavefly
 
Introduction to corona sdk
Introduction to corona sdkIntroduction to corona sdk
Introduction to corona sdk馬 萬圳
 

What's hot (18)

Groovy:Candy for Java Developers
Groovy:Candy for Java DevelopersGroovy:Candy for Java Developers
Groovy:Candy for Java Developers
 
Python匯出入csv以及繪製圖表初稿
Python匯出入csv以及繪製圖表初稿Python匯出入csv以及繪製圖表初稿
Python匯出入csv以及繪製圖表初稿
 
全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版
 
Introduction To Direct Show
Introduction To Direct ShowIntroduction To Direct Show
Introduction To Direct Show
 
3 Python开发风格与建议
3 Python开发风格与建议3 Python开发风格与建议
3 Python开发风格与建议
 
Nio trick and trap
Nio trick and trapNio trick and trap
Nio trick and trap
 
Ptyhon 教學 003 函數
Ptyhon 教學 003 函數Ptyhon 教學 003 函數
Ptyhon 教學 003 函數
 
Golang server design pattern
Golang server design patternGolang server design pattern
Golang server design pattern
 
嵌入式平台移植技巧概說
嵌入式平台移植技巧概說嵌入式平台移植技巧概說
嵌入式平台移植技巧概說
 
Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command line
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享
 
Light talk @ coscup 2011 : Incremental Global Prelink for Android
Light talk @ coscup 2011 : Incremental Global Prelink for AndroidLight talk @ coscup 2011 : Incremental Global Prelink for Android
Light talk @ coscup 2011 : Incremental Global Prelink for Android
 
少年科技人雜誌 2015 年八月
少年科技人雜誌 2015 年八月少年科技人雜誌 2015 年八月
少年科技人雜誌 2015 年八月
 
Build Your Own Android Toolchain from scratch
Build Your Own Android Toolchain from scratchBuild Your Own Android Toolchain from scratch
Build Your Own Android Toolchain from scratch
 
Hcsm lect-20120913
Hcsm lect-20120913Hcsm lect-20120913
Hcsm lect-20120913
 
Golangintro
GolangintroGolangintro
Golangintro
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析
 
Introduction to corona sdk
Introduction to corona sdkIntroduction to corona sdk
Introduction to corona sdk
 

Similar to 用Cython封装c++代码为python模块的一点经验

DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代scott liao
 
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509tidesq
 
[3]投影片 futurewad樹莓派研習會 141204
[3]投影片 futurewad樹莓派研習會 141204[3]投影片 futurewad樹莓派研習會 141204
[3]投影片 futurewad樹莓派研習會 141204CAVEDU Education
 
LineBot.pptx
LineBot.pptxLineBot.pptx
LineBot.pptxNCUDSC
 
容器與 Gitlab CI 應用
容器與 Gitlab CI 應用容器與 Gitlab CI 應用
容器與 Gitlab CI 應用Philip Zheng
 
Continuous Delivery Workshop with Ansible x GitLab CI (3rd)
Continuous Delivery Workshop with Ansible x GitLab CI (3rd)Continuous Delivery Workshop with Ansible x GitLab CI (3rd)
Continuous Delivery Workshop with Ansible x GitLab CI (3rd)Chu-Siang Lai
 
Python 入门
Python 入门Python 入门
Python 入门kuco945
 
C++工程实践
C++工程实践C++工程实践
C++工程实践Shuo Chen
 
竞赛中C++语言拾遗
竞赛中C++语言拾遗竞赛中C++语言拾遗
竞赛中C++语言拾遗乐群 陈
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introductionotakustay
 
深入浅出Netty l.t
深入浅出Netty   l.t深入浅出Netty   l.t
深入浅出Netty l.toleone
 
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUGYingSiang Geng
 
Bitbucket pipeline CI
Bitbucket pipeline CIBitbucket pipeline CI
Bitbucket pipeline CIZero Huang
 
Python速成指南
Python速成指南Python速成指南
Python速成指南March Liu
 
Python meetup 1
Python meetup 1Python meetup 1
Python meetup 1Vic Yang
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xBo-Yi Wu
 
基于 FRIDA 的全平台逆向分析
基于 FRIDA 的全平台逆向分析基于 FRIDA 的全平台逆向分析
基于 FRIDA 的全平台逆向分析CC
 
Continuous Delivery with Ansible x GitLab CI (2e)
Continuous Delivery with Ansible x GitLab CI (2e)Continuous Delivery with Ansible x GitLab CI (2e)
Continuous Delivery with Ansible x GitLab CI (2e)Chu-Siang Lai
 
推薦系統實作
推薦系統實作推薦系統實作
推薦系統實作FEG
 

Similar to 用Cython封装c++代码为python模块的一点经验 (20)

DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
 
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
 
[3]投影片 futurewad樹莓派研習會 141204
[3]投影片 futurewad樹莓派研習會 141204[3]投影片 futurewad樹莓派研習會 141204
[3]投影片 futurewad樹莓派研習會 141204
 
LineBot.pptx
LineBot.pptxLineBot.pptx
LineBot.pptx
 
容器與 Gitlab CI 應用
容器與 Gitlab CI 應用容器與 Gitlab CI 應用
容器與 Gitlab CI 應用
 
Continuous Delivery Workshop with Ansible x GitLab CI (3rd)
Continuous Delivery Workshop with Ansible x GitLab CI (3rd)Continuous Delivery Workshop with Ansible x GitLab CI (3rd)
Continuous Delivery Workshop with Ansible x GitLab CI (3rd)
 
Python 入门
Python 入门Python 入门
Python 入门
 
C++工程实践
C++工程实践C++工程实践
C++工程实践
 
竞赛中C++语言拾遗
竞赛中C++语言拾遗竞赛中C++语言拾遗
竞赛中C++语言拾遗
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introduction
 
深入浅出Netty l.t
深入浅出Netty   l.t深入浅出Netty   l.t
深入浅出Netty l.t
 
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
 
Ch01
Ch01Ch01
Ch01
 
Bitbucket pipeline CI
Bitbucket pipeline CIBitbucket pipeline CI
Bitbucket pipeline CI
 
Python速成指南
Python速成指南Python速成指南
Python速成指南
 
Python meetup 1
Python meetup 1Python meetup 1
Python meetup 1
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.x
 
基于 FRIDA 的全平台逆向分析
基于 FRIDA 的全平台逆向分析基于 FRIDA 的全平台逆向分析
基于 FRIDA 的全平台逆向分析
 
Continuous Delivery with Ansible x GitLab CI (2e)
Continuous Delivery with Ansible x GitLab CI (2e)Continuous Delivery with Ansible x GitLab CI (2e)
Continuous Delivery with Ansible x GitLab CI (2e)
 
推薦系統實作
推薦系統實作推薦系統實作
推薦系統實作
 

More from Leo Zhou

第三名 3rd zhyict
第三名 3rd zhyict第三名 3rd zhyict
第三名 3rd zhyictLeo Zhou
 
异常检测在苏宁的实践
异常检测在苏宁的实践异常检测在苏宁的实践
异常检测在苏宁的实践Leo Zhou
 
第二名 2nd 火眼金睛
第二名 2nd 火眼金睛第二名 2nd 火眼金睛
第二名 2nd 火眼金睛Leo Zhou
 
第四名 4th H3C AI Institute
第四名 4th H3C AI Institute第四名 4th H3C AI Institute
第四名 4th H3C AI InstituteLeo Zhou
 
第一名 1st Bocoiops
第一名 1st Bocoiops第一名 1st Bocoiops
第一名 1st BocoiopsLeo Zhou
 
第六名 6th Aurora
第六名 6th Aurora第六名 6th Aurora
第六名 6th AuroraLeo Zhou
 
AI使能网络自动驾驶 AI Building Autonomous Driving Network
AI使能网络自动驾驶 AI Building Autonomous Driving NetworkAI使能网络自动驾驶 AI Building Autonomous Driving Network
AI使能网络自动驾驶 AI Building Autonomous Driving NetworkLeo Zhou
 
2.2 go在阿里云cdn系统的应用
2.2 go在阿里云cdn系统的应用2.2 go在阿里云cdn系统的应用
2.2 go在阿里云cdn系统的应用Leo Zhou
 
1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb1.6 米嘉 gobuildweb
1.6 米嘉 gobuildwebLeo Zhou
 
1.4 go在数据存储上面的应用—毛剑
1.4 go在数据存储上面的应用—毛剑1.4 go在数据存储上面的应用—毛剑
1.4 go在数据存储上面的应用—毛剑Leo Zhou
 
1.2 刘奇 go在分布式数据库中的应用
1.2 刘奇 go在分布式数据库中的应用1.2 刘奇 go在分布式数据库中的应用
1.2 刘奇 go在分布式数据库中的应用Leo Zhou
 
Protocol libraries the right way
Protocol libraries the right wayProtocol libraries the right way
Protocol libraries the right wayLeo Zhou
 
美团数据库运维平台介绍
美团数据库运维平台介绍美团数据库运维平台介绍
美团数据库运维平台介绍Leo Zhou
 
特卖场景下的大数据平台和机器学习实践
特卖场景下的大数据平台和机器学习实践特卖场景下的大数据平台和机器学习实践
特卖场景下的大数据平台和机器学习实践Leo Zhou
 
我的互联网运维理论与实践
我的互联网运维理论与实践我的互联网运维理论与实践
我的互联网运维理论与实践Leo Zhou
 
如何选择 Docker 监控方案
如何选择 Docker 监控方案如何选择 Docker 监控方案
如何选择 Docker 监控方案Leo Zhou
 
美团数据库运维平台介绍
美团数据库运维平台介绍美团数据库运维平台介绍
美团数据库运维平台介绍Leo Zhou
 
The net is dark and full of terrors - James Bennett
The net is dark and full of terrors - James BennettThe net is dark and full of terrors - James Bennett
The net is dark and full of terrors - James BennettLeo Zhou
 
Hypothesis randomised testing for django
Hypothesis randomised testing for djangoHypothesis randomised testing for django
Hypothesis randomised testing for djangoLeo Zhou
 
NoSQL@VIP — 唯品会NoSQL平台⾃动化发展及运维经验分享
NoSQL@VIP — 唯品会NoSQL平台⾃动化发展及运维经验分享NoSQL@VIP — 唯品会NoSQL平台⾃动化发展及运维经验分享
NoSQL@VIP — 唯品会NoSQL平台⾃动化发展及运维经验分享Leo Zhou
 

More from Leo Zhou (20)

第三名 3rd zhyict
第三名 3rd zhyict第三名 3rd zhyict
第三名 3rd zhyict
 
异常检测在苏宁的实践
异常检测在苏宁的实践异常检测在苏宁的实践
异常检测在苏宁的实践
 
第二名 2nd 火眼金睛
第二名 2nd 火眼金睛第二名 2nd 火眼金睛
第二名 2nd 火眼金睛
 
第四名 4th H3C AI Institute
第四名 4th H3C AI Institute第四名 4th H3C AI Institute
第四名 4th H3C AI Institute
 
第一名 1st Bocoiops
第一名 1st Bocoiops第一名 1st Bocoiops
第一名 1st Bocoiops
 
第六名 6th Aurora
第六名 6th Aurora第六名 6th Aurora
第六名 6th Aurora
 
AI使能网络自动驾驶 AI Building Autonomous Driving Network
AI使能网络自动驾驶 AI Building Autonomous Driving NetworkAI使能网络自动驾驶 AI Building Autonomous Driving Network
AI使能网络自动驾驶 AI Building Autonomous Driving Network
 
2.2 go在阿里云cdn系统的应用
2.2 go在阿里云cdn系统的应用2.2 go在阿里云cdn系统的应用
2.2 go在阿里云cdn系统的应用
 
1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb
 
1.4 go在数据存储上面的应用—毛剑
1.4 go在数据存储上面的应用—毛剑1.4 go在数据存储上面的应用—毛剑
1.4 go在数据存储上面的应用—毛剑
 
1.2 刘奇 go在分布式数据库中的应用
1.2 刘奇 go在分布式数据库中的应用1.2 刘奇 go在分布式数据库中的应用
1.2 刘奇 go在分布式数据库中的应用
 
Protocol libraries the right way
Protocol libraries the right wayProtocol libraries the right way
Protocol libraries the right way
 
美团数据库运维平台介绍
美团数据库运维平台介绍美团数据库运维平台介绍
美团数据库运维平台介绍
 
特卖场景下的大数据平台和机器学习实践
特卖场景下的大数据平台和机器学习实践特卖场景下的大数据平台和机器学习实践
特卖场景下的大数据平台和机器学习实践
 
我的互联网运维理论与实践
我的互联网运维理论与实践我的互联网运维理论与实践
我的互联网运维理论与实践
 
如何选择 Docker 监控方案
如何选择 Docker 监控方案如何选择 Docker 监控方案
如何选择 Docker 监控方案
 
美团数据库运维平台介绍
美团数据库运维平台介绍美团数据库运维平台介绍
美团数据库运维平台介绍
 
The net is dark and full of terrors - James Bennett
The net is dark and full of terrors - James BennettThe net is dark and full of terrors - James Bennett
The net is dark and full of terrors - James Bennett
 
Hypothesis randomised testing for django
Hypothesis randomised testing for djangoHypothesis randomised testing for django
Hypothesis randomised testing for django
 
NoSQL@VIP — 唯品会NoSQL平台⾃动化发展及运维经验分享
NoSQL@VIP — 唯品会NoSQL平台⾃动化发展及运维经验分享NoSQL@VIP — 唯品会NoSQL平台⾃动化发展及运维经验分享
NoSQL@VIP — 唯品会NoSQL平台⾃动化发展及运维经验分享
 

用Cython封装c++代码为python模块的一点经验

  • 2. 内容提要   •  自我介绍   •  Cython是什么?   •  Cython有什么能力?   •  一点使用经验   •  Q&A   2  
  • 3. 自我介绍   •  赖勇浩,Python  的重度用户,PyCon  的老朋友。   •  从业10年,创立广州齐昌网络科技有限公司,主 营技术咨询与开发外包。   •  合著有《编写高质量代码:改善Python程序的91 个建议》一书。   3  
  • 4. Cython是什么?   •  Cython  is  an  optimising  static   compiler  for  both  the  Python   programming  language  and  the   extended  Cython  programming   language  (based  on  Pyrex).     4  
  • 5. Cython是什么?   •  It  makes  writing  C  extensions  for   Python  as  easy  as  Python  itself.     – Cython  can  compile  (most)  regular   Python  code   – It  aims  to  become  a  superset  of  the   [Python]  language  which  gives  it   high-­‐level,  object-­‐oriented,   functional,  and  dynamic  programming.   5  
  • 6. Cython有什么能力?   •  无痛提升运行效率。   •  Python程序防逆向工程。   •  一点工作,数(百)倍运行效率。   •  把好用的Python库给C/C++用。   •  更方便、更简单地编写Python扩展。   •  封装已有的C/C++库。   6  
  • 7. 无痛提升运行效率。   def  f(x):          return  x**2-­‐x     def  integrate_f(a,b,N):          s  =  0          dx  =  (b-­‐a)/N          for  i  in  range(N):                  s  +=  f(a+i*dx)          return  s  *  dx   cython  ex1.py     gcc  -­‐fPIC  -­‐shared  -­‐I/ usr/include/python2.7   ex1.c  -­‐o  ex1.so   7  
  • 9. 一点工作,数(百)倍运行效率。   def  f(double  x):          return  x**2-­‐x     def  integrate_f(double  a,  double  b,  int  N):          cdef  int  i          cdef  double  s,  dx          s  =  0          dx  =  (b-­‐a)/N          for  i  in  range(N):                  s  +=  f(a+i*dx)          return  s  *  dx   9  
  • 10. 一点工作,数(百)倍运行效率。   def  f(double  x):          return  x**2-­‐x     cdef  double  f(double  x)  except?  -­‐2:          return  x**2-­‐x   10  
  • 11. 把好用的Python库给C/C++用。   #  pyurllib.pyx     import  urllib     cdef  public  char  *  urlopen(char  *   url):          content=                  urllib.urlopen(url).read()          return  content     11  
  • 12. 把好用的Python库给C/C++用。   #include  <Python.h>   #include  "pyurllib.h"     int  main  (int  argc,  char  **   argv)   {      /*  Boiler  plate  init  Python   */      Py_SetProgramName  (argv   [0]);      Py_Initialize  ();        /*  Init  our  url  module  into   Python  memory  */      initpyurllib();      if  (argc  >=  2)          {              /*  call  directly  into   our  cython  module  */                 printf("%s",urlopen(argv[1]));          }      else          printf  ("require  url... n");        /*  cleanup  python  before   exit  ...  */      Py_Finalize  ();        return  0;   }   12  
  • 13. 更方便、更简单地编写Python扩展。   //  fic.c   #include  “Python.h”   static  PyObject*  fib(...)  {   //  解释参数   //  C  类型的实参转变为Python对象   //  业务逻辑代码   //  构建返回值的Python对象          return  ret;   }   static  PyMethodDef  fib_methods[]  =  {          {“fib”,  fib,  ...},          {NULL,NULL,0,NULL},   };   void  initfib(void)  {          (void)  Py_InitModule(...);   }   #setup.py   module=Extension(‘fib’,   sources=[‘fib.c’])   setup(name=‘fib’,    version=‘1.0,   ext_modules=[module])     $  python  setup.py  build_ext  –inplace     >>>  import  fib   >>>  fib.fib(2000)   1  1  2  3  5  8  13  21  34  55  89  144  233   377  610  987  1597     13  
  • 14. 更方便、更简单地编写Python扩展。   #  fib.pyx   def  fib(n):          a,  b  =  0,  1          while  b  <  n:                  print  b,                  a,  b  =  b,  a  +  b     from  distutils.core  import   setup   from  Cython.Build  import   cythonize     setup(           ext_modules=cythonize("fib.pyx "),   )   $  python  setup.py  build_ext  – inplace     >>>  import  fib   >>>  fib.fib(2000)   1  1  2  3  5  8  13  21  34  55  89  144   233  377  610  987  1597     14  
  • 15. 封装已有的C/C++库。   •  选择:ctypes/CFFI/boost.python/ SWIG/cython   •  优势:   – 易学,Python+C=Cython,重用旧知识   – 好用,pxd,重用声明文件   – C++支持全面,可从C++中回调Python函数, 为Python  class重载C++  class行为提供可能   – ……   15  
  • 16. 一点使用经验   •  经历了一个项目:封装C&C++编写的数据库 驱动为Python模块。   – 导出类型定义、宏与结构体   – 导出函数、函数类型与复杂的类   – 在Python类中使用C++类成员变量   – 错误码与异常的转换   – 编写一层C++代码简化接口   16  
  • 17. 导出类型定义、宏与结构体   cdef  extern  from  "ossTypes.h":          ctypedef  char  CHAR            ctypedef  unsigned  long   long  UINT64          ctypedef  long  long  SINT64          ctypedef  long  long  INT64     cdef  extern  from  "ossErr.h":          enum:SDB_OK          enum:SDB_DMS_EOC     cdef  extern  from  "msg.h":          ctypedef  struct   MsgSysInfoRequest:                  pass          ctypedef  struct   MsgSysInfoReply:                  pass          ctypedef  struct   MsgOpReply:                  INT32  field_u_need     17  
  • 18. 导出函数、函数类型与复杂的类   cdef  extern  from  "_sdbapi.h"  namespace  "qc":          SINT32  steal_size(const  CHAR*)          ctypedef  void  (*network_func)(CHAR*  buff,  size_t   size)            cdef  cppclass  ConnectionContext:                  ConnectionContext()  except  +                    CHAR*  send_buff                  INT32  send_buff_size                  network_func  sender                  network_func  recver                    string  str()   18  
  • 19. 在Python类中使用C++类成员变量   import  socket   cdef  class  Connection(object):          cdef  ConnectionContext*  _ctx          cdef  object  _sock          def  __cinit__(self):                  self._ctx  =  new  ConnectionContext()                  self._sock  =  socket.socket()          def  __dealloc__(self):                  del  self._ctx   19  
  • 20. 错误码与异常的转换   •  需要有统一的错误处理机制,C/C++  的错 误码方式需要转换为Python异常。   class  Error(StandardError):          def  __init__(self,  msg,  err=None):                  self.err  =  err                  self.msg  =  msg                            def  __str__(self):                  return  'Error(err=%s,  msg="%s")'  %   (self.err,  self.msg)     20  
  • 21. 编写一层C++代码简化接口   •  避免需要把复杂的类用Cython声明一遍   •  避免C++代码风格传导到Python   •  更一致的生命周期管理   •  编译器、平台相关的需求(如#pragma   pack)   •  胶合层提供更高的灵活性   21  
  • 22. 关于齐昌   客户   技术   22