# Shared Memory Python example

**URL:** <https://community.br-automation.com/t/shared-memory-python-example/4642>\
**Category:** Code & Libraries\
**Tags:** hypervisor, arsim, toolkit\
**Created:** [October 22, 2024, 11:16am UTC](https://community.br-automation.com/t/shared-memory-python-example/4642 "2024-10-22T11:16:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![christiaan.cloet](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christiaan.cloet/32/1975_2.png) [@christiaan.cloet](https://community.br-automation.com/u/christiaan.cloet)\
**Post date:** [October 22, 2024, 11:16am UTC](https://community.br-automation.com/t/shared-memory-python-example/4642/1 "2024-10-22T11:16:10Z")

</div>

For HyperVisor and ArSim there is the possibility for using shared memory.  
On the AR side this is provided with libraries ArIscShm and ArIscEvent  
On the GPOS / Host side there is the brisc.dll

The AS help comes with a ST example for AR and a c example for GPOS/host

This is a Python script that is performing the same task as the c example.  
It’s not perfect, but it works. Tested with AS 6.0 and Python 3.11

Also thanks to @corne.geerts , I used parts of his code.

```auto
import ctypes
from ctypes import cdll, c_char_p, c_void_p, POINTER, c_long
import time
import keyboard
Handle = ctypes.c_int
AR_EVENT_NAME = "AR_Event"
GPOS_EVENT_NAME = "GPOS_Event"
# Execute ShmEcho sample program first by setting start=TRUE. Then execute this script
if __name__ == " __main__":
    try:
        c_lib = ctypes.CDLL(".\\brisc.dll") # x64\\brisc.dll with python3
        ShmOpen = c_lib.briscShmOpen
        ShmOpen.restype = ctypes.POINTER(ctypes.c_int) # pointer to int
        # open shared memory
        Handle = ShmOpen()
        print("Handle:",Handle)
        # get size of memory
        ShmGetSize = c_lib.briscShmGetSize
        Size = ShmGetSize(Handle)
        print ("Size: ", Size)
        # get address of memory
        ShmGetAddress = c_lib.briscShmGetAddress
        ShmGetAddress.restype = ctypes.POINTER(ctypes.c_char * Size) 
        DataAddress = ShmGetAddress(Handle)
        # create GPOS event
        print("Create event GPOS: ", end="")
        ShmCreateEvent = c_lib.briscEventCreate
        ShmCreateEvent.restype = ctypes.POINTER(ctypes.c_long)
        hEventGPOS = ShmCreateEvent(GPOS_EVENT_NAME.encode('utf-8') )        
        print ("hEventGPOS: ",hEventGPOS)
        # open AR event
        print("Open event made by AR: ", end="")
        ShmOpenEvent = c_lib.briscEventOpen
        ShmOpenEvent.restype = ctypes.POINTER(ctypes.c_long)
        start_time = time.time()
        while time.time() - start_time < 2:
            hEventAR = ShmOpenEvent(AR_EVENT_NAME.encode('utf-8'))
            if hEventAR != 0:
                break
            print("... ", end="")
            time.sleep(1)
        print("hEventAR: ",hEventAR)
        # wait for the GPOS event to be triggered by AR
        BriscEventWait = c_lib.briscEventWait
        BriscEventWait.argtypes = [ctypes.POINTER(ctypes.c_long),ctypes.c_ulong]
        if hEventGPOS != 0:
            Triggered = BriscEventWait(hEventGPOS,2000)
        # get string value from shared memory
        data = ctypes.string_at(DataAddress, 10)
        print(data.decode('utf-8'))
        # write back to memory
        new_data = "Hello AR".encode('utf-8') + b'\0'
        if len(new_data)<Size:
            ctypes.memmove(DataAddress, new_data, len(new_data))
        # trigger the event created by AR
        if hEventAR != 0:
            BriscEventTrigger = c_lib.briscEventTrigger
            BriscEventTrigger.argtypes = [POINTER(c_long)]
            BriscEventTrigger(hEventAR)
        print("Press any key to continue...")
        keyboard.read_event()
    finally:
        c_lib.briscShmClose(Handle)
        if hEventGPOS != 0:
            c_lib.briscEventClose(hEventGPOS)
            print("Event Closed")

```

---

<div class="post-metadata">

**Author:** ![kovarj](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/kovarj/32/537_2.png) [@kovarj](https://community.br-automation.com/u/kovarj)\
**Post date:** [October 22, 2024, 7:14pm UTC](https://community.br-automation.com/t/shared-memory-python-example/4642/2 "2024-10-22T19:14:16Z")

</div>

cool. I will definitely try it, thanks for sharing 🙂

---

<div class="post-metadata">

**Author:** ![jayD](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/jayd/32/1676_2.png) [@jayD](https://community.br-automation.com/u/jayD)\
**Post date:** [October 25, 2024, 12:35am UTC](https://community.br-automation.com/t/shared-memory-python-example/4642/3 "2024-10-25T00:35:26Z")

</div>

Thanks for sharing 🙂

---

<div class="post-metadata">

**Author:** ![christiaan.cloet](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christiaan.cloet/32/1975_2.png) [@christiaan.cloet](https://community.br-automation.com/u/christiaan.cloet)\
**Post date:** [October 25, 2024, 11:38am UTC](https://community.br-automation.com/t/shared-memory-python-example/4642/4 "2024-10-25T11:38:31Z")

</div>

Thanks! Looking forward to hear your experience!

---

<div class="post-metadata">

**Author:** ![tommi.piiparinen](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/tommi.piiparinen/32/2183_2.png) [@tommi.piiparinen](https://community.br-automation.com/u/tommi.piiparinen)\
**Post date:** [June 24, 2025, 5:58am UTC](https://community.br-automation.com/t/shared-memory-python-example/4642/5 "2025-06-24T05:58:28Z")

</div>

I just tested this and it works pretty much plug and play.

All I had do to to get this running was to install the keyboard pip package.

```auto
pip install keyboard

```
