Random thoughts shooting out of volatile mind
Python and C Coupling
One of the best feature of Python is you can write some modules in C and use it in the Python. This is usually done for the parts of the program which requires intensive calculation and their implementation in Python may make entire application slow. For coupling you need to write the function which are calculation intensive in C and create a shared library file of it. Then you can use the ctypes module in Python to import those module into your Python program. Well as an example lets write an C function for calculating factorial. See the code below

Lets call this file test.c Now compile this file with following commands
gcc -fpic test.c
gcc -shared -o libtest.so test.o

This creates a shared library (dll in Windows way). Now lets write a Python file with same function to calculate factorial. Why I'm doing this is to show the efficiency of using C coupling. Below is the sample code I used.

Well don't worry I purposefully did factorial of 1000 to show what happens. Python will run out of stack space and if you remove the try except block python will complain about stack depth exceeded and finally quits, but C function works pretty fine and returns a result. Well I don't know if result is proper or not since finding result wasn't my concern. Here is the output you get when you run the program
Python recursive call error stack depth exceeded
C call from python 0.062844
You can change the number and test the time taken. One thing I want to tell here is CDLL is for *nix users, if you are Windows user you should use either WinDLL or OleDLL class for doing this. Any variable you pass to the C functions need to be of types defined in ctypes you can't directly pass a Python data type to C functions.
There is no proper example available on how to use this in documentation so thought of writing this blog. I'm still learning the ways of utilizing C programs from with in Python. And as I learn new things I'll update them here.
Posted by: copyninja on Monday, 5 July 2010

blog comments powered by Disqus
Fork me on GitHub