在架构服务器的时候,使用了IOCP来收发数据,使用Python写解包入库程序,那么要让两个进程通信,有很多方法,这里使用了内存共享的方法
python本来没有内存共享(我没有找到,也不知道是不是真的没有,但是和C++的程序共享内存,我想确实没有的吧),那么只好自己用C写个扩展,方法记录下来,以备后用
首先是扩展的*.c程序
MEM.c
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
接下来写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
运行这个程序:
![](https://i-blog.csdnimg.cn/blog_migrate/fad1bcb6912c8174cfb9ae5857ae2ab5.bmp)
已经获得了正确的输出
转载于:https://blog.51cto.com/fireflyliu/131975