Skip to content

Commit 4ddb2f3

Browse files
committed
Return future from execution
1 parent e7b34ac commit 4ddb2f3

4 files changed

Lines changed: 26 additions & 18 deletions

File tree

src/executorlib/task_scheduler/interactive/blockallocation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def _execute_multiple_tasks(
317317
f.set_exception(exception=interface_initialization_exception)
318318
else:
319319
# The interface failed during the execution
320-
interface.status = execute_task_dict(
320+
interface.status, _ = execute_task_dict(
321321
task_dict=task_dict,
322322
future_obj=f,
323323
interface=interface,

src/executorlib/task_scheduler/interactive/onetoone.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def _execute_task_in_thread(
262262
worker_id (int): Communicate the worker which ID was assigned to it for future reference and resource
263263
distribution.
264264
"""
265-
if not execute_task_dict(
265+
status, _ = execute_task_dict(
266266
task_dict=task_dict,
267267
future_obj=future_obj,
268268
interface=interface_bootup(
@@ -277,7 +277,8 @@ def _execute_task_in_thread(
277277
cache_directory=cache_directory,
278278
cache_key=cache_key,
279279
error_log_file=error_log_file,
280-
):
280+
)
281+
if status is False:
281282
future_obj.set_exception(
282283
ExecutorlibSocketError("SocketInterface crashed during execution.")
283284
)

src/executorlib/task_scheduler/interactive/shared.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import time
55
from concurrent.futures import Future
66
from concurrent.futures._base import PENDING
7-
from typing import Optional
7+
from typing import Optional, Tuple
88

99
from executorlib.standalone.interactive.communication import (
1010
ExecutorlibSocketError,
@@ -20,7 +20,7 @@ def execute_task_dict(
2020
cache_directory: Optional[str] = None,
2121
cache_key: Optional[str] = None,
2222
error_log_file: Optional[str] = None,
23-
) -> bool:
23+
) -> Tuple[bool, dict]:
2424
"""
2525
Execute the task in the task_dict by communicating it via the interface.
2626
@@ -37,6 +37,7 @@ def execute_task_dict(
3737
3838
Returns:
3939
bool: True if the task was submitted successfully, False otherwise.
40+
dict: Dictionary of nested tasks if any were returned by the executed function, otherwise an empty dictionary.
4041
"""
4142
if not future_obj.done() and future_obj.set_running_or_notify_cancel():
4243
if error_log_file is not None:
@@ -54,7 +55,7 @@ def execute_task_dict(
5455
future_obj=future_obj,
5556
)
5657
else:
57-
return True
58+
return True, {}
5859

5960

6061
def task_done(future_queue: queue.Queue):
@@ -88,7 +89,7 @@ def reset_task_dict(future_obj: Future, future_queue: queue.Queue, task_dict: di
8889

8990
def _execute_task_without_cache(
9091
interface: SocketInterface, task_dict: dict, future_obj: Future
91-
) -> bool:
92+
) -> Tuple[bool, dict]:
9293
"""
9394
Execute the task in the task_dict by communicating it via the interface.
9495
@@ -101,14 +102,17 @@ def _execute_task_without_cache(
101102
Returns:
102103
bool: True if the task was submitted successfully, False otherwise.
103104
"""
105+
new_task_dict = {}
104106
output = interface.send_and_receive_dict(input_dict=task_dict)
105107
if "result" in output:
106108
future_obj.set_result(output["result"])
109+
if "tasks_nested" in output:
110+
new_task_dict = output["tasks_nested"]
107111
elif isinstance(output["error"], ExecutorlibSocketError):
108-
return False
112+
return False, new_task_dict
109113
else:
110114
future_obj.set_exception(exception=output["error"])
111-
return True
115+
return True, new_task_dict
112116

113117

114118
def _execute_task_with_cache(
@@ -117,7 +121,7 @@ def _execute_task_with_cache(
117121
future_obj: Future,
118122
cache_directory: str,
119123
cache_key: Optional[str] = None,
120-
) -> bool:
124+
) -> Tuple[bool, dict]:
121125
"""
122126
Execute the task in the task_dict by communicating it via the interface using the cache in the cache directory.
123127
@@ -139,6 +143,7 @@ def _execute_task_with_cache(
139143
resource_dict=task_dict.get("resource_dict", {}),
140144
cache_key=cache_key,
141145
)
146+
new_task_dict = {}
142147
file_name = os.path.abspath(os.path.join(cache_directory, task_key + "_o.h5"))
143148
if file_name not in get_cache_files(cache_directory=cache_directory):
144149
time_start = time.time()
@@ -148,11 +153,13 @@ def _execute_task_with_cache(
148153
data_dict["runtime"] = time.time() - time_start
149154
dump(file_name=file_name, data_dict=data_dict)
150155
future_obj.set_result(output["result"])
156+
if "tasks_nested" in output:
157+
new_task_dict = output["tasks_nested"]
151158
elif isinstance(output["error"], ExecutorlibSocketError):
152-
return False
159+
return False, new_task_dict
153160
else:
154161
future_obj.set_exception(exception=output["error"])
155162
else:
156163
_, _, result = get_output(file_name=file_name)
157164
future_obj.set_result(result)
158-
return True
165+
return True, new_task_dict

tests/unit/task_scheduler/interactive/test_shared.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_execute_task_sum(self):
4343
cache_directory=None,
4444
cache_key=None,
4545
error_log_file=None,
46-
)
46+
)[0]
4747
self.assertTrue(result)
4848
self.assertTrue(f.done())
4949
self.assertEqual(f.result(), 3)
@@ -71,7 +71,7 @@ def test_execute_task_done(self):
7171
cache_directory=None,
7272
cache_key=None,
7373
error_log_file=None,
74-
)
74+
)[0]
7575
self.assertTrue(result)
7676
self.assertTrue(f.done())
7777
self.assertEqual(f.result(), 5)
@@ -98,7 +98,7 @@ def test_execute_task_error(self):
9898
cache_directory=None,
9999
cache_key=None,
100100
error_log_file=None,
101-
)
101+
)[0]
102102
self.assertFalse(result)
103103
self.assertFalse(f.done())
104104

@@ -132,7 +132,7 @@ def test_execute_task_sum(self):
132132
cache_directory="cache_execute_task",
133133
cache_key=None,
134134
error_log_file=None,
135-
)
135+
)[0]
136136
self.assertTrue(result)
137137
self.assertTrue(f.done())
138138
self.assertEqual(f.result(), 3)
@@ -160,7 +160,7 @@ def test_execute_task_done(self):
160160
cache_directory="cache_execute_task",
161161
cache_key=None,
162162
error_log_file=None,
163-
)
163+
)[0]
164164
self.assertTrue(result)
165165
self.assertTrue(f.done())
166166
self.assertEqual(f.result(), 5)
@@ -187,6 +187,6 @@ def test_execute_task_error(self):
187187
cache_directory="cache_execute_task",
188188
cache_key=None,
189189
error_log_file=None,
190-
)
190+
)[0]
191191
self.assertFalse(result)
192192
self.assertFalse(f.done())

0 commit comments

Comments
 (0)