Skip to content

Instantly share code, notes, and snippets.

@evandrocoan
Last active July 25, 2023 08:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandrocoan/bbd3aa06435428025f5a6c913aaa99ba to your computer and use it in GitHub Desktop.
Save evandrocoan/bbd3aa06435428025f5a6c913aaa99ba to your computer and use it in GitHub Desktop.
Example of shared memory usage in python using mmap!
import mmap
import time
import sys
import json

# https://docs.python.org/3.9/library/mmap.html
# https://blog.askesis.pl/post/2019/02/mmap.html
memory = mmap.mmap( -1, 20, access=mmap.ACCESS_WRITE )
memory.write( b'{ "var": 10 }' )

end_of_the_file = memory.tell()
print( 'memory {:,}, end_of_the_file {}\n'.format( len( memory ), end_of_the_file ), file=sys.stderr )

memory.seek( 0 )
print( 'raw', memory.read( end_of_the_file ), file=sys.stderr )

memory.seek( 0 )
loaded = json.loads( memory.read( end_of_the_file ) )

print( 'json', loaded, file=sys.stderr )
print( 'loaded', memory[:end_of_the_file], file=sys.stderr )

time.sleep( 20 )

-->

memory 20, end_of_the_file 13

raw b'{ "var": 10 }'
json {'var': 10}
loaded b'{ "var": 10 }'
[Finished in 20.1s]
@evandrocoan
Copy link
Author

import mmap
from multiprocessing.shared_memory import SharedMemory
import subprocess
import os

# Create a shared memory string
shared_str = "Hello from parent process!".encode("utf-8")
filesize = len(shared_str) * 4

shared_mem = SharedMemory(name='MyMemory', size=1024, create=True)
shared_mem.buf[0] = 1
shared_mem.buf[1] = 2

# deck_json_bin = deck_json.encode("utf-8")
# filesize = len(deck_json_bin)
# shared_mem = SharedMemory(name='MyMemory', size=filesize, create=True)
# shared_mem.buf[:filesize] = deck_json_bin

with open("hello.txt", "wb") as f:
    f.write(b"Hello Python!\n")

with open("hello.txt", "r+b") as f, \
        mmap.mmap(f.fileno(), filesize, access=mmap.ACCESS_WRITE) as memoryfile:
    memoryfile.write(shared_str)

    # Start the subprocess script
    python_script = f"""
import mmap

from multiprocessing.shared_memory import SharedMemory
shared_mem = SharedMemory(name='MyMemory', create=False)
print(shared_mem.buf[0])
print(shared_mem.buf[1])

# Access the shared memory string
with open("hello.txt", "r+b") as f, \
        mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as memoryfile:
    shared_str = memoryfile.read().decode("utf-8")
    # jsonobject = json.loads(bytes(shared_mem.buf[:{filesize}]).decode("utf-8"))
    print(shared_str)
"""
    with subprocess.Popen(
        ["python", "-u", "-c", python_script],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        env=os.environ,
    ) as process:
        # Read the output from the subprocess
        output = process.stdout.read().decode("utf-8")
        print(output)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment