1. python读取共享内存数据时出现乱码
1. Python文件设置编码 utf-8 (文件前面加上 #encoding=utf-8)
2. MySQL数据库charset=utf-8
3. Python连接MySQL是加上参数 charset=utf8
4. 设置Python的默认编码为 utf-8 (sys.setdefaultencoding(utf-8)
2. python中,为什么采取赋值共享内存的方式
通过share memory 取对象的例子, c write object into memory map, python read it by call dll api.
So there still questions you should consider how to guarantee the process share security. Good luck..
----python part----
3. 如何利用python实现类似c语言的共同体
import ctypes #你可以看看ctypes,它可以支持union,下面是一个例子
from ctypes import ( Union, Array,
c_uint8, c_uint32,
cdll, CDLL
)
class uint8_array(Array):
_type_ = c_uint8
_length_ = 4
class u_type(Union):
_fields_ = ("data", c_uint32), ("chunk", uint8_array)
# load printf function from Dynamic Linked Libary libc.so.6 (I'm use linux)
libc = CDLL(cdll.LoadLibrary('libc.so.6')._name)
printf = libc.printf
if __name__ == "__main__": # initialize union
_32bitsdata = u_type() # set values to chunk
_32bitsdata.chunk[:] = (1, 2, 3, 4) # and print it
printf(b"Data in 32 bits: %d\n", _32bitsdata.data)
4. c可以调用python吗
可以的。
C中内嵌Python
新建立一个工程,首先需要将工作目录设置到Python-3.1.1PCbuild中,以获取到动态库,至于静态库的包含,Include目录的指定,那自然也是少不了的。文件中需要包含Python.h文件,这也是必须的。
接口中
Py_Initialize();
Py_Finalize();
其他的根据需求,再引入相应的python builder 即可
5. 用python多进程模块multiprocessing创建的子进程如何共享内存空间
进程传递数据最简单方便的是通过Queue。这样你的自建类对象就可以放到队列中,由子进程获取。
到于Array, Var等方法,那是给高效数据共享用的。共享内存是进程通信的高级技巧。需要高性能计算的时候再研究这些方法。
Pool, Manager之类是一种封装。用得反而比较少。
python与C++共享内存里,还会使用一种Numpy中的数组。那个效率更高。
你的程序中子进程及传递参数都没有问题。你少了一句。在后面要加上
p.join()就可以了
如果不加,那么你的主进程不等子进程,它先退出了,往往操作系统会自动把子进程也杀掉。
另外子进程中的print输出有延时。即使你用sys.stdout.flush(),有时候它也会有延时。