Skip to content

Commit 3388a07

Browse files
author
Dimitar Tasev
committed
Adds a unique prefix for all allocations of this instance
Only deletes them on shutdown. Adds test Fixes #745
1 parent 06937ac commit 3388a07

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

mantidimaging/core/parallel/test/utility_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
# SPDX - License - Identifier: GPL-3.0-or-later
33

44
import mock
5+
import numpy as np
56

6-
from mantidimaging.core.parallel.utility import multiprocessing_necessary, execute_impl
7+
from mantidimaging.core.parallel.utility import create_array, free_all_owned_by_this_instance, multiprocessing_necessary, execute_impl
8+
import SharedArray as sa
79

810

911
def test_correctly_chooses_parallel():
@@ -38,6 +40,21 @@ def test_execute_impl_par(mock_pool):
3840
assert mock_progress.update.call_count == 15
3941

4042

43+
def test_free_all_owned_by_this_instance():
44+
[sa.delete(x.name.decode("utf-8")) for x in sa.list()]
45+
create_array((10, 10), np.float32, random_name=True)
46+
create_array((10, 10), np.float32, random_name=True)
47+
create_array((10, 10), np.float32, random_name=True)
48+
49+
temp_name = "not_this_instance"
50+
sa.create("not_this_instance", (10, 10))
51+
52+
assert len(sa.list()) == 4
53+
free_all_owned_by_this_instance()
54+
assert len(sa.list()) == 1
55+
sa.delete(temp_name)
56+
57+
4158
if __name__ == "__main__":
4259
import pytest
4360

mantidimaging/core/parallel/utility.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@
2424

2525
NP_DTYPE = Type[np.single]
2626

27+
INSTANCE_PREFIX = str(uuid.uuid4())
28+
29+
30+
def free_all_owned_by_this_instance():
31+
for arr in [array for array in sa.list() if array.name.decode("utf-8").startswith(INSTANCE_PREFIX)]:
32+
sa.delete(arr.name.decode("utf-8"))
33+
2734

2835
def create_shared_name(file_name=None) -> str:
29-
return f"{uuid.uuid4()}{f'-{os.path.basename(file_name)}' if file_name is not None else ''}"
36+
return f"{INSTANCE_PREFIX}-{uuid.uuid4()}{f'-{os.path.basename(file_name)}' if file_name is not None else ''}"
3037

3138

3239
def delete_shared_array(name, silent_failure=False):

mantidimaging/main.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import argparse
66
import atexit
77
import logging
8+
from mantidimaging.core.parallel.utility import INSTANCE_PREFIX, free_all_owned_by_this_instance
89
import warnings
910

1011
import SharedArray as sa
@@ -42,11 +43,7 @@ def parse_args():
4243

4344

4445
def main():
45-
def free_all():
46-
for arr in sa.list():
47-
sa.delete(arr.name.decode("utf-8"))
48-
49-
atexit.register(free_all)
46+
atexit.register(free_all_owned_by_this_instance)
5047
args = parse_args()
5148
# Print version number and exit
5249
if args.version:
@@ -57,7 +54,6 @@ def free_all():
5754

5855
h.initialise_logging(logging.getLevelName(args.log_level))
5956
startup_checks()
60-
free_all()
6157

6258
from mantidimaging import gui
6359
gui.execute()

0 commit comments

Comments
 (0)