Skip to content

Instantly share code, notes, and snippets.

@yohm
Last active August 27, 2017 12:44
Show Gist options
  • Save yohm/57524dd17dd4bb44ec7ac24c66d468da to your computer and use it in GitHub Desktop.
Save yohm/57524dd17dd4bb44ec7ac24c66d468da to your computer and use it in GitHub Desktop.
communication between cpp process and python process using pipe
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#define READ (0)
#define WRITE (1)
/**
* @param *fd_r 読み込み用ファイルディスクリプタ
* @param *fd_w 書き込み用ファイルディスクリプタ
*/
int popen2(int *fd_r, int *fd_w) {
// 子から親への通信用パイプ
int pipe_child2parent[2];
// 親から子への通信用パイプ
int pipe_parent2child[2];
// プロセスID
int pid;
if (pipe(pipe_child2parent) != 0 ||
pipe(pipe_parent2child) != 0 ||
(pid = fork()) < 0
) {
perror("popen2");
close(pipe_child2parent[READ]);
close(pipe_child2parent[WRITE]);
close(pipe_parent2child[READ]);
close(pipe_parent2child[WRITE]);
return 1;
}
// 子プロセスか?
if (pid == 0) {
// 子プロセスの場合は、親→子への書き込みはありえないのでcloseする
close(pipe_parent2child[WRITE]);
// 子プロセスの場合は、子→親の読み込みはありえないのでcloseする
close(pipe_child2parent[READ]);
// 親→子への出力を標準入力として割り当て
dup2(pipe_parent2child[READ], 0);
// 子→親への入力を標準出力に割り当て
dup2(pipe_child2parent[WRITE], 1);
// 割り当てたファイルディスクリプタは閉じる
close(pipe_parent2child[READ]);
close(pipe_child2parent[WRITE]);
// 子プロセスはここで該当プログラムを起動しリターンしない
if (execlp("python", "python", "-u", "test.py", NULL) < 0) {
perror("popen2");
close(pipe_parent2child[READ]);
close(pipe_child2parent[WRITE]);
return 1;
}
}
// 親プロセス側の処理
close(pipe_parent2child[READ]);
close(pipe_child2parent[WRITE]);
*fd_r = pipe_child2parent[READ];
*fd_w = pipe_parent2child[WRITE];
return pid;
}
void lntrim(char *str) {
char *p;
p = strchr(str, '\n');
if(p != NULL) {
*p = '\0';
}
}
void read_task(FILE* fp_r) {
size_t buf_size = 256;
char* buf = (char*) malloc(buf_size);
int len = getline( &buf, &buf_size, fp_r );
lntrim(buf);
while( strlen(buf) > 0 ) {
printf("test2: %s\n", buf);
len = getline( &buf, &buf_size, fp_r );
lntrim(buf);
}
printf("read task end\n");
free(buf);
}
/////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
int fd_r, fd_w;
popen2(&fd_r, &fd_w);
FILE* fp_r = fdopen(fd_r, "r");
FILE* fp_w = fdopen(fd_w, "w");
if( fp_w == NULL ) { std::cout << "NULL" << std::endl; }
read_task(fp_r);
for( int i=0; i<5; i++) {
sleep(1);
const char* msg = "abc\n";
fprintf(fp_w, "abc%d\n", i);
fflush(fp_w);
read_task(fp_r);
}
fclose(fp_r);
fclose(fp_w);
return 0;
}
from __future__ import print_function
import sys
def submit(i,cmd,point,seed):
s_point = " ".join( [str(x) for x in point] )
out = "%d %s %d %s %d" % (i,cmd,i,s_point,seed)
print(out)
for i in range(3):
submit(i,"./foobar.out",(i+1,i+2,3-i), i+1000)
print("")
while True:
line = sys.stdin.readline()
if not line:
break
submit(1,line.rstrip(),(2,3,4),1234)
print("")
sys.stderr.write("ending python\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment