{"id":20801928,"url":"https://github.com/philips-software/pyhighspeedsharedmemory","last_synced_at":"2025-06-11T21:06:59.136Z","repository":{"id":63562562,"uuid":"547181967","full_name":"philips-software/PyHighSpeedSharedMemory","owner":"philips-software","description":"A pure python library that wraps the multiprocess SharedMemory that simplies code for high speed data exchange between processes","archived":false,"fork":false,"pushed_at":"2023-03-20T09:34:37.000Z","size":18,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-12T02:47:14.325Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/philips-software.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-07T09:07:59.000Z","updated_at":"2025-02-13T12:09:55.000Z","dependencies_parsed_at":"2024-11-17T18:27:04.408Z","dependency_job_id":"8d2ead7e-0a96-4f58-ada5-e377cba4c659","html_url":"https://github.com/philips-software/PyHighSpeedSharedMemory","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/philips-software/PyHighSpeedSharedMemory","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philips-software%2FPyHighSpeedSharedMemory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philips-software%2FPyHighSpeedSharedMemory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philips-software%2FPyHighSpeedSharedMemory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philips-software%2FPyHighSpeedSharedMemory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/philips-software","download_url":"https://codeload.github.com/philips-software/PyHighSpeedSharedMemory/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philips-software%2FPyHighSpeedSharedMemory/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259340654,"owners_count":22843034,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-17T18:26:11.752Z","updated_at":"2025-06-11T21:06:59.116Z","avatar_url":"https://github.com/philips-software.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PyHighSpeedSharedMemory\n\nThis repository is for the package: memory_interface.\nThis package contains the classes by which high speed data can be exchanged between two processes. \n\nThe basic concept is to have two classes of the same type, exchanging data with each other at high speeds, \neach running in a different process. In Python it is not possible to distribute an initialized class between\ntwo processes. For this reason, what is distributed are Python Dictionaries that contain the \"state\" of the class \ninstead of the class itself. So on one size, a given class is initialized and it passes its state to \nan uninitialized version of that same class which has been launched in a different process. In the state\nof both classes is a pointer to the same multiprocessing.shared_memory.Sharedmemory block.\n\nPython's Shared memory https://docs.python.org/3/library/multiprocessing.shared_memory.html\nencapsulates semaphores, so the read and write actions to a shared memory, written by a user\ndoes not contain any checks; it can be read and written to as if it was a local variable. \n\nThis repository contains classes that wrap the Shared memory class and contain interfaces to \npass the classes initialized Shared memory to a class of the same type. \n\nThere are classes to exchange primitive types of data:\n- class SharedBytes\n- class SharedInt\n- class SharedBool\n- class SharedString\n\nAnd class MemoryInterface, which is a circular memory of user-defined data chunks. \nThis circular memory structure has its own \"semaphores\", which a user must use, to\nensure that data is not overwritten and that only valid data is read. \n\nAll of the classes SharedXXX share the basic structure:\n\n````python\n     class xxx:\n       def __init__(self, memblk=None):\n       # this gives the possiblit to quickly initiize this class with the state in \"memblk\"\n\n       def create_sharedobject(self, *kwargs) -\u003e object:\n       # this method creates a shared object; the classes \"states\" to be exchanged\n       # this returns the classes \"state\"\n\n       def load_sharedobject(self, memblk):\n       #this method loads the state contained in memblk\n\n       def read(self):\n       # this method performs a read of the shared object and returns it\n\n       def write(self, num):\n       # this method writes num to the shared object.\n       # it will throw a ValueError if it is to large or is None\n\n       def cleanup(self):\n       # to be clalled by the creator of this class\n````\nTheir typical Use: The initiator creates the shared object and then passes it on to a process\n```python\nimport time\nfrom multiprocessing import Process\nfrom memory_interface import SharedString\n\nINITIAL_STRING = \"Hello\"\nANSWER = \"From afar\"\n\ndef runningstr(memblk):\n    # jump start a local version:\n    localstr = SharedString(memblk)\n    first_string = localstr.read()\n    assert first_string == INITIAL_STRING\n    localstr.write(ANSWER)  # give an answer\n    print(f\"From afar read {first_string} and is sending {ANSWER}\")\n\n\ndef Initialize():\n    sharedstring = SharedString()\n    memblk = sharedstring.create_sharedobject(200)  # the shared string will be 200 charactgers in length\n\n    sharedstring.write(INITIAL_STRING)  # initialize it (must be less than 200)\n\n    proc = Process(target=runningstr, args=[memblk])  # passes the memblk\n    proc.start()\n    \n    busy = True\n    while busy:\n        answer = sharedstring.read()\n        if answer == INITIAL_STRING:\n            print(\"Waiting....\")\n            time.sleep(1)\n        else:\n            busy = False\n\n    assert answer == ANSWER\n    print(f\"In main process read: \u003c{answer}\u003e\")\n\n    proc.join()\n    sharedstring.cleanup()\n\nif __name__ == \"__main__\":\n    Initialize()\n```\n \nThe class MemoryInterface has its own \"semaphores\", which a user can use, to\nensure that data is not overwritten and that only valid data is read. \n\nThe \"can_read\"  method is to be called before the \"read\" method to ensure that only\nvalid data is read.\n\nThe \"can_write\" method is to be called before the \"write\" method to ensure that valid\ndata is not overwritten. \n\nThe method \"get_fullness\" can be used to monitor the filling of the circular memory\n\n````python\n     class MemoryInterface:\n     \n       def __init__(self, memblk=None):\n       # this gives the possibility to quickly initiize this class with the state in \"memblk\"\n\n       def create_memorybuf(self, largest_object=[i for i in range(10)], nr_of_memories=10, use_pickle=True):\n       # this method creates a shared object; the classes \"states\" to be exchanged\n       # this returns the classes \"state\"\n       # Must use pickel, unless largest_objest are bytes. \n\n       def load_memorybuf(self, memblk):\n       # this method loads the state contained in memblk\n       \n       def can_read(self)-\u003ebool:\n       # this is the semaphore. Only if True, then read may be performed\n\n       def read(self):\n       # this method performs a read of the shared object and returns it\n\n       def can_write(self)-\u003ebool:\n       # this is the semaphore: write only if this is true\n       \n       def write(self, num):\n       # this method writes num to the shared object.\n       # it will throw a ValueError if it is to large or is None\n\n       def cleanup(self):\n       # to be clalled by the creator of this class\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilips-software%2Fpyhighspeedsharedmemory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphilips-software%2Fpyhighspeedsharedmemory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilips-software%2Fpyhighspeedsharedmemory/lists"}