How to hand over information to a program organized in C with Python

Asked 2 years ago, Updated 2 years ago, 42 views

I'm making a multi-threading program in C language. Python can't do it because it has quite a lot of computation. Most of all, we've implemented all of the key functions.

This C language program needs to put the data in the beginning. There are about 10 intasers and about 100 double variables. Now I read this from the csv file in my computer.

This csv file reads DB in Python and drops it. Yes... I didn't know how to do ODBC or something, so I chose Python because I was lazy -_-;;;;

DB ->Python ->C.

The Python program calls the C program using the OS module, so that process works automatically.

Oh, and the Python program is not just dropping the DB, but it's also acting as a server and always responding to network responses.

The C program drops five computed result-double arrays into the csv file. The Python program reads it and writes it to the DB. Eventually, DB-> Python-> File->C-> File-> Python->DB became a very unreasonable structure;;;;;

This is what I know how to do, so there is a process of unnecessarily writing and reading, rewriting and rereading in the file, what should I do?

How do you get around 100 factors to program C with Python? How can I get the result of program C?

There are only a few results, so I don't know how to transfer the factor even if I just get stdout.

How do you do it?

c python

2022-09-21 16:35

1 Answers

If communication is between independent processes, traditional IPC methods can be used.

It can be handled using messages, pipes, shared memory, sockets, etc.

However, if it is between Python and c, it is best to make an expansion module or process it using ffi (the Python is provided with ctypes).

Looking at the question, it seems that the library written in c is simply called from Python....

I think it would be better to make an extension module that surrounds the library with cython.


2022-09-21 16:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.