Skip to content

Commit 58a3870

Browse files
committed
[Refactor] receive_dict() returns the status and the error as dict
1 parent 7346897 commit 58a3870

4 files changed

Lines changed: 24 additions & 28 deletions

File tree

src/executorlib/standalone/interactive/communication.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ def receive_dict(self) -> dict:
8686
self._logger.warning(
8787
"Received dictionary of size: " + str(sys.getsizeof(data))
8888
)
89-
output = cloudpickle.loads(data)
90-
if "result" in output:
91-
return output["result"]
92-
else:
93-
raise output["error"]
89+
return cloudpickle.loads(data)
9490

9591
def send_and_receive_dict(self, input_dict: dict) -> dict:
9692
"""
@@ -154,7 +150,7 @@ def shutdown(self, wait: bool = True):
154150
if self._spawner.poll():
155151
result = self.send_and_receive_dict(
156152
input_dict={"shutdown": True, "wait": wait}
157-
)
153+
)["result"]
158154
self._spawner.shutdown(wait=wait)
159155
self._reset_socket()
160156
return result

src/executorlib/task_scheduler/interactive/blockallocation.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,9 @@ def _set_init_function(
379379
) -> Optional[Exception]:
380380
interface_initialization_exception = None
381381
if init_function is not None and interface.status:
382-
try:
383-
_ = interface.send_and_receive_dict(
384-
input_dict={"init": True, "fn": init_function, "args": (), "kwargs": {}}
385-
)
386-
except Exception as init_exception:
387-
interface_initialization_exception = init_exception
382+
output = interface.send_and_receive_dict(
383+
input_dict={"init": True, "fn": init_function, "args": (), "kwargs": {}}
384+
)
385+
if "error" in output:
386+
interface_initialization_exception = output["error"]
388387
return interface_initialization_exception

src/executorlib/task_scheduler/interactive/shared.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,14 @@ def _execute_task_without_cache(
101101
Returns:
102102
bool: True if the task was submitted successfully, False otherwise.
103103
"""
104-
try:
105-
future_obj.set_result(interface.send_and_receive_dict(input_dict=task_dict))
106-
except Exception as thread_exception:
107-
if isinstance(thread_exception, ExecutorlibSocketError):
104+
output = interface.send_and_receive_dict(input_dict=task_dict)
105+
if "result" in output:
106+
future_obj.set_result(output["result"])
107+
else:
108+
if isinstance(output["error"], ExecutorlibSocketError):
108109
return False
109110
else:
110-
future_obj.set_exception(exception=thread_exception)
111+
future_obj.set_exception(exception=output["error"])
111112
return True
112113

113114

@@ -141,18 +142,18 @@ def _execute_task_with_cache(
141142
)
142143
file_name = os.path.abspath(os.path.join(cache_directory, task_key + "_o.h5"))
143144
if file_name not in get_cache_files(cache_directory=cache_directory):
144-
try:
145-
time_start = time.time()
146-
result = interface.send_and_receive_dict(input_dict=task_dict)
147-
data_dict["output"] = result
145+
time_start = time.time()
146+
output = interface.send_and_receive_dict(input_dict=task_dict)
147+
if "result" in output:
148+
data_dict["output"] = output["result"]
148149
data_dict["runtime"] = time.time() - time_start
149150
dump(file_name=file_name, data_dict=data_dict)
150151
future_obj.set_result(result)
151-
except Exception as thread_exception:
152-
if isinstance(thread_exception, ExecutorlibSocketError):
152+
else:
153+
if isinstance(output["error"], ExecutorlibSocketError):
153154
return False
154155
else:
155-
future_obj.set_exception(exception=thread_exception)
156+
future_obj.set_exception(exception=output["error"])
156157
else:
157158
_, _, result = get_output(file_name=file_name)
158159
future_obj.set_result(result)

tests/unit/standalone/interactive/test_communication.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_interface_mpi(self):
6363
)
6464
self.assertTrue(interface.status)
6565
self.assertEqual(
66-
interface.send_and_receive_dict(input_dict=task_dict), np.array(4)
66+
interface.send_and_receive_dict(input_dict=task_dict)["result"], np.array(4)
6767
)
6868
interface.shutdown(wait=True)
6969

@@ -96,7 +96,7 @@ def test_interface_serial_without_debug(self):
9696
)
9797
self.assertTrue(interface.status)
9898
self.assertEqual(
99-
interface.send_and_receive_dict(input_dict=task_dict), np.array(4)
99+
interface.send_and_receive_dict(input_dict=task_dict)["result"], np.array(4)
100100
)
101101
interface.shutdown(wait=True)
102102

@@ -129,7 +129,7 @@ def test_interface_serial_with_debug(self):
129129
)
130130
self.assertTrue(interface.status)
131131
self.assertEqual(
132-
interface.send_and_receive_dict(input_dict=task_dict), np.array(4)
132+
interface.send_and_receive_dict(input_dict=task_dict)["result"], np.array(4)
133133
)
134134
interface.shutdown(wait=True)
135135

@@ -195,7 +195,7 @@ def test_interface_serial_with_stopped_process(self):
195195
interface.send_dict(input_dict=task_dict)
196196
interface._spawner._process.terminate()
197197
with self.assertRaises(ExecutorlibSocketError):
198-
interface.receive_dict()
198+
raise interface.receive_dict()["error"]
199199

200200

201201
class TestZMQ(unittest.TestCase):

0 commit comments

Comments
 (0)