say i have some code in C which is fast and specific but i want to manage how its called in python .i have seen python modules written in c like numpy how would i do that? and is this possible for all compiled languages like rust/go etc.
for example i have a really fast custom database orm in c++ with multithreading and all that good stuff. how would i convert it into a python library and use it? i would like this as python isn't as fast as or as versatile as C++ etc.
if this is possible how does it work in the back ?
Here are the steps:
.so)More detailed answer:
Write file in C:
// test.c
int add_numbers(int a, int b) {
return a + b;
}
Compile and generate shared library:
gcc -shared -o test.so test.c
Load using ctypes:
import ctypes
lib = ctypes.CDLL('./test.so')
Call those methods (This step has the most overhead)
lib.add_numbers.argtypes = (ctypes.c_int, ctypes.c_int)
lib.add_numbers.restype = ctypes.c_int
result = lib.add_numbers(3, 4)
print(result)
Good read:
NB: doing this for a DB will require lots of attention, and experiment; as you need to compile it. Trust me it takes lots of time. Then you need to add these method wrapper in Python.
Yes, you can do the opposite also, you need to have <Python.h> header in your program. But trust me, it's more work. But if it required for some reason, yeah there is atleast a way, of course.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With