Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Shared Memory Cleanup #340

Merged
merged 18 commits into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions backend/staticfiles/synthesis/main.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import importlib
import json
import functools
import multiprocessing
import random
import string
from multiprocessing import shared_memory, Lock
from time import sleep
import signal
import sys

from lib.inputs import Inputs
from lib.outputs import Outputs
from lib.parameters import Parameters
from lib.utils import Synchronise


BLOCK_DIRECTORY = 'modules'
FUNCTION_NAME = 'main'


def clean_shared_memory(names):
def clean_shared_memory(signum, frame, names, processes):
# End all processes
for process in processes:
process.terminate()
process.join()

all_names = list(names.keys())
all_names.extend([name + "_dim" for name in names])
all_names.extend([name + "_shape" for name in names])
Expand All @@ -38,6 +45,10 @@ def clean_shared_memory(names):
except ValueError:
pass

# Exit the program
print("Exiting program.")
sys.exit(0)


def main():
"""
Expand Down Expand Up @@ -116,17 +127,18 @@ def main():
multiprocessing.Process(target=method, args=(inputs, outputs, parameters, Synchronise(1 / (freq if freq != 0 else 30))))
)

# Register handler for Ctrl+C
param_func = functools.partial(clean_shared_memory, names=all_wires, processes=processes)
signal.signal(signal.SIGINT, param_func)

for process in processes:
process.start()

try:
while True:
sleep(10)
except KeyboardInterrupt:
for process in processes:
process.terminate()
process.join()
clean_shared_memory(all_wires)
pass


if __name__ == "__main__":
Expand Down
Loading