内部のいくつかのユーティリティ関数を使用して.DLL
、共有メモリを「名前付き」で作成します
struct
次のプロトタイプを持つ関数を使用して、Python経由で次のCを読み書きする必要があります。
int write_shmem(const char* shmem_name, char* shmem)
int read_shmem ( const char* shmem_name , char* shmem);
.DLL
ファイル内
shmem_name
共有メモリ名shmem
書き込むデータです
Cstruct
はに似ています
typedef struct {
unsigned char c;
unsigned long l;
float f;
double d;
} DataBlock ;
私はPythonコードで以下を使用しています
from ctypes import *
dll = cdll.LoadLibrary('shared_mem.dll')
write_shmem = dll.write_shmem
write_shmem.restype = ctypes.c_int
read_shmem = dll.read_shmem
read_shmem.restype = ctypes.c_int
class DataBlock(Structure):
_fields_ = [('c', c_ubyte), ('l', c_ulong),
('f',c_float), ('d', c_double) ]
data = DataBlock('A',123, 12.32, 1.89721)
write_shmem(c_char_p(b'P0W') , ??? ) # cast(data, POINTER(c_char) ?
#...
read_shmem(c_char_p(b'P0W'), ??? ) # cast(data, POINTER(c_char) ?
に型キャストする方法はchar*
?
キャストを使用すると、データが共有メモリに正しく配置されますか? C アプリケーションから同じ共有メモリを読み取る必要がある
編集
使用:
int create_shmem(const char*, long long );
.DLL
「名前付き」共有メモリを作成しています
Python コード:
create_shmem( c_char_p(b'P0W'),
ctypes.c_longlong(sizeof(DataBlock)) )
write_shmem
read_shmem
作成されたメモリのサイズを使用して、データの書き込み/読み取りを 行うだけです。これは.DLL
関数自体で行われます。(Boost
ここではプロセス間共有メモリ オブジェクトを使用します)
Python バージョンの使用: 3.3.0