Skip to content

Commit faf71ea

Browse files
[Refactor] receive_dict() returns the status and the error as dict (#1029)
* [Refactor] receive_dict() returns the status and the error as dict * fix test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix ruff * return error rather than crashing --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6dfe717 commit faf71ea

4 files changed

Lines changed: 33 additions & 37 deletions

File tree

src/executorlib/standalone/interactive/communication.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,17 @@ def receive_dict(self) -> dict:
7878
while len(response_lst) == 0:
7979
response_lst = self._poller.poll(self._time_out_ms)
8080
if not self._spawner.poll():
81-
raise ExecutorlibSocketError(
82-
"SocketInterface crashed during execution."
83-
)
81+
return {
82+
"error": ExecutorlibSocketError(
83+
"SocketInterface crashed during execution."
84+
),
85+
}
8486
data = self._socket.recv(zmq.NOBLOCK)
8587
if self._logger is not None:
8688
self._logger.warning(
8789
"Received dictionary of size: " + str(sys.getsizeof(data))
8890
)
89-
output = cloudpickle.loads(data)
90-
if "result" in output:
91-
return output["result"]
92-
else:
93-
raise output["error"]
91+
return cloudpickle.loads(data)
9492

9593
def send_and_receive_dict(self, input_dict: dict) -> dict:
9694
"""
@@ -154,7 +152,7 @@ def shutdown(self, wait: bool = True):
154152
if self._spawner.poll():
155153
result = self.send_and_receive_dict(
156154
input_dict={"shutdown": True, "wait": wait}
157-
)
155+
)["result"]
158156
self._spawner.shutdown(wait=wait)
159157
self._reset_socket()
160158
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: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ 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):
108-
return False
109-
else:
110-
future_obj.set_exception(exception=thread_exception)
104+
output = interface.send_and_receive_dict(input_dict=task_dict)
105+
if "result" in output:
106+
future_obj.set_result(output["result"])
107+
elif isinstance(output["error"], ExecutorlibSocketError):
108+
return False
109+
else:
110+
future_obj.set_exception(exception=output["error"])
111111
return True
112112

113113

@@ -141,18 +141,17 @@ def _execute_task_with_cache(
141141
)
142142
file_name = os.path.abspath(os.path.join(cache_directory, task_key + "_o.h5"))
143143
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
144+
time_start = time.time()
145+
output = interface.send_and_receive_dict(input_dict=task_dict)
146+
if "result" in output:
147+
data_dict["output"] = output["result"]
148148
data_dict["runtime"] = time.time() - time_start
149149
dump(file_name=file_name, data_dict=data_dict)
150-
future_obj.set_result(result)
151-
except Exception as thread_exception:
152-
if isinstance(thread_exception, ExecutorlibSocketError):
153-
return False
154-
else:
155-
future_obj.set_exception(exception=thread_exception)
150+
future_obj.set_result(output["result"])
151+
elif isinstance(output["error"], ExecutorlibSocketError):
152+
return False
153+
else:
154+
future_obj.set_exception(exception=output["error"])
156155
else:
157156
_, _, result = get_output(file_name=file_name)
158157
future_obj.set_result(result)

tests/unit/standalone/interactive/test_communication.py

Lines changed: 5 additions & 5 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

@@ -194,8 +194,8 @@ def test_interface_serial_with_stopped_process(self):
194194
self.assertTrue(interface.status)
195195
interface.send_dict(input_dict=task_dict)
196196
interface._spawner._process.terminate()
197-
with self.assertRaises(ExecutorlibSocketError):
198-
interface.receive_dict()
197+
output = interface.receive_dict()
198+
self.assertIsInstance(output["error"], ExecutorlibSocketError)
199199

200200

201201
class TestZMQ(unittest.TestCase):

0 commit comments

Comments
 (0)