在架构服务器的时候,使用了IOCP来收发数据,使用Python写解包入库程序,那么要让两个进程通信,有很多方法,这里使用了内存共享的方法
python本来没有内存共享(我没有找到,也不知道是不是真的没有,但是和C++的程序共享内存,我想确实没有的吧),那么只好自己用C写个扩展,方法记录下来,以备后用
首先是扩展的*.c程序
MEM.c
#include "Python.h"
#include <windows.h>
#include <stdio.h>
int ShareMemory(char arg[],char str[]){
HANDLE hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
FALSE,
arg
);
if(hMapFile == NULL){
return 1;
}
LPVOID lpMapAddress = MapViewOfFile(
hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
0
);
if(lpMapAddress == NULL){
return 1;
}
// cout << (char *)lpMapAddress << endl;
lstrcpy(str,(char *)lpMapAddress);
UnmapViewOfFile(lpMapAddress);
return 0;
}
static PyObject *
PyExt_fac(PyObject *self,PyObject *args){
char *str;
char str1[4096];
PyObject *retval;
if(!PyArg_ParseTuple(args,"s",&str));
ShareMemory(str,str1);
retval = (PyObject *)Py_BuildValue("s",str1);
return retval;
}
static PyMethodDef
PyExtMethods[] = {
{"fac",PyExt_fac,METH_VARARGS},
{NULL,NULL}
};
void initPyExt(){
Py_InitModule("PyExt",PyExtMethods);
}
#include <windows.h>
#include <stdio.h>
int ShareMemory(char arg[],char str[]){
HANDLE hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
FALSE,
arg
);
if(hMapFile == NULL){
return 1;
}
LPVOID lpMapAddress = MapViewOfFile(
hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
0
);
if(lpMapAddress == NULL){
return 1;
}
// cout << (char *)lpMapAddress << endl;
lstrcpy(str,(char *)lpMapAddress);
UnmapViewOfFile(lpMapAddress);
return 0;
}
static PyObject *
PyExt_fac(PyObject *self,PyObject *args){
char *str;
char str1[4096];
PyObject *retval;
if(!PyArg_ParseTuple(args,"s",&str));
ShareMemory(str,str1);
retval = (PyObject *)Py_BuildValue("s",str1);
return retval;
}
static PyMethodDef
PyExtMethods[] = {
{"fac",PyExt_fac,METH_VARARGS},
{NULL,NULL}
};
void initPyExt(){
Py_InitModule("PyExt",PyExtMethods);
}
接下来写setup.py编译
from distutils.core import setup,Extension
MOD = 'PyExt'
setup(name=MOD,ext_modules=[
Extension(MOD,sources=['MEM.c'])])
MOD = 'PyExt'
setup(name=MOD,ext_modules=[
Extension(MOD,sources=['MEM.c'])])
在命令行下键入:
setup.py build -c mingw32
如果安装了vs2003那么只要这样
setup.py build
如果看到这样的提示:
running build
running build_ext
building 'PyExt' extension
D:\Qt\QtCreator\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall -ID:\Python25\include -ID:\Python25\PC
-c MEM.c -o build\temp.win32-2.5\Release\mem.o
writing build\temp.win32-2.5\Release\PyExt.def
D:\Qt\QtCreator\mingw\bin\gcc.exe -mno-cygwin -shared -s build\temp.win32-2.5\Release\mem.o build\te
mp.win32-2.5\Release\PyExt.def -LD:\Python25\libs -LD:\Python25\PCBuild -lpython25 -lmsvcr71 -o buil
d\lib.win32-2.5\PyExt.pyd
running build_ext
building 'PyExt' extension
D:\Qt\QtCreator\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall -ID:\Python25\include -ID:\Python25\PC
-c MEM.c -o build\temp.win32-2.5\Release\mem.o
writing build\temp.win32-2.5\Release\PyExt.def
D:\Qt\QtCreator\mingw\bin\gcc.exe -mno-cygwin -shared -s build\temp.win32-2.5\Release\mem.o build\te
mp.win32-2.5\Release\PyExt.def -LD:\Python25\libs -LD:\Python25\PCBuild -lpython25 -lmsvcr71 -o buil
d\lib.win32-2.5\PyExt.pyd
说明编译成功了,编译后的.pyc文件在build\lib.win32-2.5目录里
吧这个.pyc文件拷贝到工程目录下,建立一个py文件来测试一下
from PyExt import fac
str2 = fac('你的共享内存申请字符串')
print str2
str2 = fac('你的共享内存申请字符串')
print str2
运行这个程序:
已经获得了正确的输出