Thursday, July 2, 2015

Raspberry pi, python에서 공유메모리 사용하기 (Python shared memory usage)

Install python sysv_ipc module

#일단, 최신버전으로 시스템 업데이트
sudo apt-get update


#python-dev 설치
sudo apt-get install python-dev

#pip 설치( 어떻게 하는지는 나도 모르겠다. 알아서 하세요)

#pip를 통해 sysv_ipc 모듈 설치
sudo pip install sysv_ipc



#이제 사용하면됨

#아래는 python예제 ( 공유 메모리 만드는 방법 )

import sysv_ipc

memory = sysv_ipc.SharedMemory( 1234, flags=01000,size=10 ,mode=0600)
#읽기
vari = memory.read()
print(vari[0])

#쓰기
memory.write("abcdefg")



#아래는 공유메모리를 얻어다 쓰는놈

import sysv_ipc

memory = sysv_ipc.SharedMemory( 1234)
#읽기
vari = memory.read()
print(vari[0])

#쓰기
memory.write("abcdefg")


-----------------------------------
#install sysv_ipc via pip
sudo pip install sysv_ipc


example (shared memory creator):

import sysv_ipc

# for creator of shared memory.     1234 is unique key
memory = sysv_ipc.SharedMemory( 1234, flags=01000,size=10 ,mode=0600)

# read
vari = memory.read()

#write
memory.write("abcdefg")



example (shared memory user):
import sysv_ipc

# for user of shared memory
memory = sysv_ipc.SharedMemory( 1234 )  # only key number needs

# read
vari = memory.read()

#write
memory.write("abcdefg")






example (shared memory creator and user):
import sysv_ipc

# open shared memory by key
memory = sysv_ipc.SharedMemory( 1234 )  # only key number needs
# not exists shared memory. so create it.
if(memory == None):
     memory = sysv_ipc.SharedMemory( 1234, flags=01000,size=10 ,mode=0600)
# read
vari = memory.read()

#write
memory.write("abcdefg")




No comments:

Post a Comment