Skip to content

Commit

Permalink
add multiprocessing argument to run_blockwise convenience function.
Browse files Browse the repository at this point in the history
if set to `False`, all blocks are run sequentially without any of the `Server`/`Client`/`Worker` interactions that can make debugging difficult
  • Loading branch information
pattonw committed Aug 27, 2024
1 parent f404286 commit a38cf72
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions daisy/convenience.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
from .cl_monitor import CLMonitor
from .server import Server
from .serial_server import SerialServer
from .tcp import IOLooper
from multiprocessing.pool import ThreadPool
from multiprocessing import Event


def run_blockwise(tasks):
def run_blockwise(tasks, multiprocessing=True):
"""Schedule and run the given tasks.
Args:
list_of_tasks:
The tasks to schedule over.
multiprocessing (bool):
If `False`, all multiprocessing is avoided and blocks are processed
sequentially. This is useful for debugging. This will only work for
tasks with a `process_function` that takes a single block as input
since a worker process would not be able to start a client and hook
up to the server.
Return:
bool:
Expand All @@ -27,16 +34,23 @@ def run_blockwise(tasks):
tasks.extend(task.upstream_tasks)

tasks = all_tasks
stop_event = Event()

IOLooper.clear()
with ThreadPool(processes=1) as pool:
result = pool.apply_async(_run_blockwise, args=(tasks, stop_event))
try:
return result.get()
except KeyboardInterrupt:
stop_event.set()
return result.get()

if not multiprocessing:
server = SerialServer()
cl_monitor = CLMonitor(server)
return server.run_blockwise(tasks)

else:
stop_event = Event()

IOLooper.clear()
with ThreadPool(processes=1) as pool:
result = pool.apply_async(_run_blockwise, args=(tasks, stop_event))
try:
return result.get()
except KeyboardInterrupt:
stop_event.set()
return result.get()


def _run_blockwise(tasks, stop_event):
Expand Down

0 comments on commit a38cf72

Please sign in to comment.