Python C++ IPC

Hallo,
hat irgendjemand eine Ahnung, ob es ein Python(3)-Modul gibt, mit dem man aus
einer schon erstellen Pipe lesen/schreiben kann.
Ich habe ein C+±Programm

// die ganzen imports...

int main(void) {
int fd[2] = {0, 0};
pipe(fd);

auto pid = fork();
if(pid == 0) {
 // ruft via system ein Python-Script auf
}
else if(pid > 0) {
 // soll mit dem Python-Script kommunizieren
}
else {
 // Fehler !
}
}

und ein Python-Script, das irgendwie mit dem C+±Programm kommunizieren soll.

Danke schon mal,
schildi48

Also wenn du suchst etwas vom stdin zu lesen brauchst du das sys Modul. Beispiel: [PYTHON]import sys
line = sys.stdin.readline()
print(line)[/PYTHON] um etwas ins stdout zu schreiben, reicht hingegen eine einfache print Operation.

Ich versuche ja nicht, auf den Bildschirm etwas auszugeben, sondern möchte einfach zwischen einem
Python und einem C++ Prozess zu kommunizieren. Der C++ Prozess forkt sich und ruft das Python-Script
auf, das dann mit dem C++ Prozess kommunizieren soll.

Tja deine geschilderte Art von Kommunikation wird nur über stdout und stdin funktionieren. Ich habe dir ein Beispiel gebaut basierend auf Example - exec and pipes um zu demonstrieren wie das funktioniert.
main.c: ```#include
#include
#include
#include

void main()
{
int pid;
int pc[2]; /* Parent to child pipe /
int cp[2]; /
Child to parent pipe */
char ch[100];
int outcount = 0;
const char *hello_from_c = "Hallo Welt von C
";

/* Make pipes */
if( pipe(pc) < 0)
{
    perror("Can't make pipe");
    exit(1);
}
if( pipe(cp) < 0)
{
    perror("Can't make pipe");
    exit(1);
}

pid = fork();
/* Create a child to run command. */
if (pid == 0 )
{
    /* Child. */
    close(STDOUT_FILENO);  /* Close current stdout. */
    dup( cp[1]);           /* Make stdout go to write
                              end of pipe. */
    close(STDIN_FILENO);   /* Close current stdin. */
    dup( pc[0]);           /* Make stdin come from read
                              end of pipe. */
    close( pc[1]);
    close( cp[0]);
    execlp("python3", "python3", "script.py");
    exit(1);
}
else if (pid > 0)
{
    /* Parent */
    write(pc[1], hello_from_c, strlen(hello_from_c));
    close(pc[1]);

    int n = read(cp[0], ch, 100);
    write(STDOUT_FILENO, ch, n);
    close(cp[0]);

    exit(0);
}
else
{
    exit(1);
}

}```
script.py: [PYTHON]import sys
line = sys.stdin.readline()
print (‘Hallo Welt von Python’)
print (line)[/PYTHON]
Grüße,
MDickie

*** Edit ***

Da du ja eigentlich wolltest, dass man die Pipes der Python Anwendung übergibt, habe ich jetzt auch noch dafür eine Lösung gefunden.

#include 
#include 
#include 
 
void main(int argc, char ** argv, char ** envp)
{
    int pid;
    int pc[2]; /* Parent to child pipe */
    int cp[2]; /* Child to parent pipe */
    char ch[100];
    int outcount = 0;
    const char *hello_from_c = "Hello from C
";

    /* Make pipes */
    if( pipe(pc) < 0)
    {
        perror("Can't make pipe");
        exit(1);
    }
    if( pipe(cp) < 0)
    {
        perror("Can't make pipe");
        exit(1);
    }

    pid = fork();
    /* Create a child to run command. */
    if (pid == 0 )
    {
        /* Child. */

        char pc_str[15];
        sprintf(pc_str, "%d", pc[0]);

        char cp_str[15];
        sprintf(cp_str, "%d", cp[1]);

        char *args[5] = {"python3", "script.py", cp_str, pc_str, NULL};
        execvp("python3", args);
        exit(1);
    }
    else if (pid > 0)
    {
        write(pc[1], hello_from_c, strlen(hello_from_c));
        close(pc[1]);

        int n = read(cp[0], ch, 100);
        write(1, ch, n);
        close(cp[0]);

        exit(0);
    }
    else
    {
        exit(1);
    }

}```
[PYTHON]import sys
import os

f = os.fdopen(int(sys.argv[2]), 'r')
line = f.readline()
f.close()

f = os.fdopen(int(sys.argv[1]), 'w')
f.write("Hello from Python
")
f.close()[/PYTHON]

Die Lösung über STDIN und STDOUT ist unpraktisch, aber die zweite ist eine Option, danke!

Gut, dann kannst du dieses Thema als gelöst markieren.