funkelab / daisy

Block-wise task scheduling for large nD volumes.
MIT License
26 stars 17 forks source link

Work on mac #53

Closed pattonw closed 3 months ago

pattonw commented 4 months ago

Make daisy run on Mac without necessitating multiprocessing.set_start_method("fork", force=True)

changes made: 1) removed all lambda functions 2) remove double underscore methods 3) use dill to pickle spawn_function in the Worker class (performance impacts unknown) 4) improved signature parsing for task process_function

Running daisy with start method spawn:

if __name__ == "__main__":

daisy.run_blockwise must be run from within a if __name__ == "__main__": block. Otherwise you will probably get this error:

RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.

This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module: ...

functools.partial

We can't use lambda functions as the process_function. If you have a function e.g. smooth(block, sigma), that you want to run for all sigma in [1, 5, 9], you have to use functools.partial to define a function with the arguments you want. so process_function = partial(smooth, sigma=1)

Here is an example that should run on mac just fine with start method "spawn":

import daisy

from functools import partial

def process_block(block,t):
    import time
    time.sleep(t)

if __name__ == "__main__":

    for t in range(3):

        task = daisy.Task(
            f"test_server_task_t{t}",
            total_roi=daisy.Roi((0,), (100,)),
            read_roi=daisy.Roi((0,), (10,)),
            write_roi=daisy.Roi((1,), (8,)),
            process_function=partial(process_block, t=t/10),
            check_function=None,
            read_write_conflict=True,
            fit="valid",
            num_workers=1,
            max_retries=2,
            timeout=None,
        )

        daisy.run_blockwise([task])
cmalinmayor commented 4 months ago

@pattonw Just putting the need for a dill dependency in writing