NEWBIE: interprocess communication between Python and C

etsang at my-deja.com etsang at my-deja.com
Sun Sep 19 21:58:16 EDT 1999


I want to test the App program's ability to do interprocess
communication. The way to do this is to have my C
driver program in one process and My App program in another process.
But in using Python extendsion to C. How can I
have the driving programs(python script, C wrapper program)  run in
one process while my Apps program in another process,

Let me put in some sample code here.
I have three files:
1) mymathtest.py as the python script
2) mymath_wrapper.c as the wrapper file to mymath.c
3) mymath.c is my App.
I can have 1) and 2) in the one process, and 3) as a seperate process?
How? Can anyone show me a concrete example, may be by modifying the
code I have below? Thanks

mymathtest.py:
#!/tools/2.5.1/local/bin/python


if __name__== '__main__':
     import sys
     import mymath_wrapper
     import struct

     print 'calling mymath_wrapper.c\n'
     print mymath_wrapper.mymath(1,2)

     print '\n'

     print 'without recomplilation, calling mymath_wrapper.c again\n'
     print mymath_wrapper.mymath(3,4)

     print 'using stucture to packed the init value before send'
     fmt = "l20sf"
     a = 250
     b = 'knight who say ni'
     c = 3.14159011841

     packed_data = struct.pack(fmt,a,b,c)
     # print struct.unpack(fmt, packed_data)
     print mymath_wrapper.mystruct(packed_data)

mymath.h:
typedef struct blockstruct
{
	int a;
	int b;
	int c;
} blockstruct;

typedef struct diffstruct
{
	int a;
	char b[20];
	float c;

} diffstruct;


blockstruct globalstruct;

diffstruct *globaldiffstruct;

int mymath(blockstruct* localstruct, int a, int b)
{
	printf("mymath is called\n");
	localstruct->a = a;
	localstruct->b = b;
	localstruct->c = a + b;
	return (int)(localstruct->c);
}

int mymath2(int a, int b)
{
	printf("mymath2 is called\n");
	globalstruct.a = a;
	globalstruct.b = b;
	globalstruct.c = a + b;
	return (int)(globalstruct.c);
}

int mystruct(diffstruct* localstruct)
{
	printf("mystruct is called\n");

	return 1;
}

mymath_wrapper.c:

#include <Python.h>
#include "mymath.h"

PyObject*
pyfunc_mymath(self, args)
 PyObject *self, *args;
 {
 PyObject *result;
 int res, a, b;
 blockstruct localstruct;

 if(!PyArg_ParseTuple(args,"ii:mymath",&a,&b))
 	return NULL;

 /* initialise a local structure */
 res = mymath(&localstruct,a,b);
 printf("localstruct a:%d b:%d c:%
d\n",localstruct.a,localstruct.b,localstruct.c);
 /* initialise a global structure, don't need to pass in address of
global struct */
 res = mymath2((a+1),(b+1));
 printf("globalstruct a:%d b:%d c:%
d\n",globalstruct.a,globalstruct.b,globalstruct.c);
 result = Py_BuildValue("i",res);

 /* python side will print the last res value*/
 return result;
 }


PyObject*
pyfunc_mystruct(self, args)
 PyObject *self, *args;
 {
 PyObject* string_from_Python;
 if(!PyArg_ParseTuple(args,"S:mymath",&string_from_Python) )
 	return NULL;
 if(PyString_Size(string_from_Python)!= sizeof(struct diffstruct))
 {
 	PyErr_SetString(PyExc_AssertionError, "Given strgin not a good
size");
 	return NULL;
 }

 globaldiffstruct = (diffstruct *)PyString_AsString(string_from_Python);
 printf("globaldiffstruct calling within C:%d  %s %
f\n",globaldiffstruct->a, globaldiffstruct->b,globaldiffstruct->c);
 /* python side will print the last res value*/
 return (PyObject *) globaldiffstruct; /* not correct, deal with this
later */
 }

 static PyMethodDef mymathMethods[] =
 {
 	{"mymath", pyfunc_mymath, 1},
 	{"mymath2", pyfunc_mymath, 1},
 	{"mystruct", pyfunc_mystruct, 1},
 	{NULL,NULL}
 };

 void initmymath_wrapper()
 {
 	(void) Py_InitModule("mymath_wrapper", mymathMethods);
 }





Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list