pass c-array via pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
/echo_vertices |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import struct, sys | |
from subprocess import Popen, PIPE | |
from threading import Thread | |
def pack(vertices, n): | |
yield struct.pack('i', n) | |
s = struct.Struct('i f f') | |
for v in vertices: | |
yield s.pack(*v) | |
def write(output_file, chunks): | |
for chunk in chunks: | |
output_file.write(chunk) | |
output_file.close() | |
def main(): | |
try: n = int(sys.argv[1]) | |
except IndexError: | |
n = 100 | |
vertices = ((i,i+1,i+2) for i in range(n)) | |
p = Popen(["./echo_vertices", "random", "arg"], stdin=PIPE, stdout=PIPE) | |
Thread(target=write, args=[p.stdin, pack(vertices, n)]).start() | |
for line in iter(p.stdout.readline, b''): | |
pass | |
p.stdout.close() | |
sys.stdout.buffer.write(line) | |
p.wait() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** http://stackoverflow.com/questions/4979421/subprocess-popen-invalid-argument-broken-pipe-while-comunicating-to-c-program */ | |
#include<stdio.h> | |
#include<stdlib.h> | |
typedef struct { | |
int index; | |
float x; | |
float y; | |
} vertex; | |
int main(int argc, char* argv[]) { | |
int i; | |
vertex* VV; | |
int n = 0; | |
fread(&n, sizeof(int), 1, stdin); | |
VV =(vertex*) malloc(sizeof(vertex)*n); | |
fread(VV,sizeof(vertex),n,stdin); | |
for(i=0;i<n;i++) | |
printf(" %i , %f , %f\n",VV[i].index,VV[i].x,VV[i].y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment