subprocess to C program

A

Aaron Brady

Hi,

I am writing a C process and I want to read data from a file that I
write to in Python. I'm creating a pipe in Python, passing it to the
C process, and calling '_read'. It gives me error 9, bad file number.

Python code:

import subprocess as s, os
r, w= os.pipe( )
os.write( w, 'abcdefghij\n' )
a= s.Popen( [ r'C:\Documents and Settings\usr\Desktop\working
\try_start', '%i'%r, '%i'%w ] )

C code:

char buf[ 16 ];
memset( buf, 0, 16 );
int readfd= atoi( argv[ 1 ] );
int ct= _read( readfd, buf, 15 );
printf( "\n\n'_read %i %i %i': %s\n", ct, readfd, errno, buf );

Output:

'_read -1 3 9'

meaning that 'ct' is -1, 'readfd' is 3, and 'errno' is 9. I want 'ct'
to be 11 at this point. Thanks in advance.
 
M

MRAB

Aaron said:
Hi,

I am writing a C process and I want to read data from a file that I
write to in Python. I'm creating a pipe in Python, passing it to the
C process, and calling '_read'. It gives me error 9, bad file number.

Python code:

import subprocess as s, os
r, w= os.pipe( )
os.write( w, 'abcdefghij\n' )
a= s.Popen( [ r'C:\Documents and Settings\usr\Desktop\working
\try_start', '%i'%r, '%i'%w ] )

C code:

char buf[ 16 ];
memset( buf, 0, 16 );
int readfd= atoi( argv[ 1 ] );
int ct= _read( readfd, buf, 15 );
printf( "\n\n'_read %i %i %i': %s\n", ct, readfd, errno, buf );

Output:

'_read -1 3 9'

meaning that 'ct' is -1, 'readfd' is 3, and 'errno' is 9. I want 'ct'
to be 11 at this point. Thanks in advance.
It looks like the ids aren't system global.
 
A

Aaron Brady

They certainly aren't in Unix: Their a property of the process.

I'm not on Unix. It has to be possible somehow. Do I need to set
permissions on the IDs? Are Stdin and Stdout my only options? Or
does Popen prevent sharing IDs somehow?
 
M

MRAB

Aaron said:
I'm not on Unix. It has to be possible somehow. Do I need to set
permissions on the IDs? Are Stdin and Stdout my only options? Or
does Popen prevent sharing IDs somehow?
You'd be better off using sockets.
 
B

bobicanprogram

You'd be better off using sockets.


You might also consider using SIMPL (http://www.icanprogram.com/simpl)
SIMPL has had a Python interface for about 5 years now. There is an
online tutorial here. (http://www.icanprogram.com/06py/main.html)

In one of your responses you mentioned that you were not on Unix (and
I presume Linux). SIMPL-Python has recently been extended to work
transparently from a Windows OS as well.

bob
SIMPL project coordinator
 
A

Aaron Brady

You might also consider using SIMPL (http://www.icanprogram.com/simpl)
SIMPL has had a Python interface for about 5 years now.   There is an
online tutorial here. (http://www.icanprogram.com/06py/main.html)

In one of your responses you mentioned that you were not on Unix (and
I presume Linux).   SIMPL-Python has recently been extended to work
transparently from a Windows OS as well.

bob
SIMPL project coordinator

The engine uses a disk-based FIFO for interprocess. It's good if
you're looking for one- or two- way sequential.
 
A

Aaron Brady

You'd be better off using sockets.

I got the 'stdin' solution to work. The Python end worked fine. I
just had to use the 'getchar' function instead of '_getch' function on
the C end. Not obvious, I guess. Ideally, I could block on input
from the console and stdin individually.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top