|  | 
|  | 1 | +from multiprocessing.managers import SyncManager | 
|  | 2 | +import multiprocessing as mp | 
|  | 3 | +import atexit, cpij.server as ijserver | 
|  | 4 | +from queue import Queue | 
|  | 5 | +from threading import Lock | 
|  | 6 | + | 
|  | 7 | + | 
|  | 8 | +class QueueManager(SyncManager): | 
|  | 9 | +    pass | 
|  | 10 | + | 
|  | 11 | + | 
|  | 12 | +QueueManager.register("input_queue") | 
|  | 13 | +QueueManager.register("output_queue") | 
|  | 14 | +QueueManager.register("get_lock") | 
|  | 15 | + | 
|  | 16 | +_init_method = None | 
|  | 17 | + | 
|  | 18 | + | 
|  | 19 | +def init_method(): | 
|  | 20 | +    global _init_method | 
|  | 21 | +    if not _init_method: | 
|  | 22 | +        if ijserver.is_server_running(): | 
|  | 23 | +            l = lock() | 
|  | 24 | +            l.acquire() | 
|  | 25 | +            to_imagej().put( | 
|  | 26 | +                {ijserver.PYIMAGEJ_KEY_COMMAND: ijserver.PYIMAGEJ_CMD_GET_INIT_METHOD} | 
|  | 27 | +            ) | 
|  | 28 | +            _init_method = from_imagej().get()[ijserver.PYIMAGEJ_KEY_OUTPUT] | 
|  | 29 | +            l.release() | 
|  | 30 | + | 
|  | 31 | +    return _init_method | 
|  | 32 | + | 
|  | 33 | + | 
|  | 34 | +def lock() -> Lock: | 
|  | 35 | +    """ | 
|  | 36 | +    Helper method to synchronzie requests with the ImageJ server. | 
|  | 37 | +
 | 
|  | 38 | +    A lock should be acquired before sending data to the server, and released after | 
|  | 39 | +    receiving the result. | 
|  | 40 | +
 | 
|  | 41 | +    Returns | 
|  | 42 | +    --------- | 
|  | 43 | +    A Lock connected to the ImageJ server. | 
|  | 44 | +    """ | 
|  | 45 | +    return _manager().get_lock() | 
|  | 46 | + | 
|  | 47 | + | 
|  | 48 | +def to_imagej() -> Queue: | 
|  | 49 | +    """ | 
|  | 50 | +    Helper method to send data to the ImageJ server | 
|  | 51 | +
 | 
|  | 52 | +    Returns | 
|  | 53 | +    --------- | 
|  | 54 | +    A Queue connected to the ImageJ server. Only its put method should be called. | 
|  | 55 | +    """ | 
|  | 56 | +    return _manager().input_queue() | 
|  | 57 | + | 
|  | 58 | + | 
|  | 59 | +def from_imagej() -> Queue: | 
|  | 60 | +    """ | 
|  | 61 | +    Helper method to retrieve data from the ImageJ server | 
|  | 62 | +
 | 
|  | 63 | +    Returns | 
|  | 64 | +    --------- | 
|  | 65 | +    A Queue connected to the ImageJ server. Only its get method should be called. | 
|  | 66 | +    """ | 
|  | 67 | +    return _manager().output_queue() | 
|  | 68 | + | 
|  | 69 | + | 
|  | 70 | +def init_pyimagej(init_string): | 
|  | 71 | +    """ | 
|  | 72 | +    Start the pyimagej daemon thread if it isn't already running. | 
|  | 73 | +
 | 
|  | 74 | +    Parameters | 
|  | 75 | +    ---------- | 
|  | 76 | +    init_string : str, optional | 
|  | 77 | +        This can be a path to a local ImageJ installation, or an initialization string per imagej.init(), | 
|  | 78 | +        e.g. sc.fiji:fiji:2.1.0 | 
|  | 79 | +    """ | 
|  | 80 | +    to_imagej().put( | 
|  | 81 | +        { | 
|  | 82 | +            ijserver.PYIMAGEJ_KEY_COMMAND: ijserver.PYIMAGEJ_CMD_START, | 
|  | 83 | +            ijserver.PYIMAGEJ_KEY_INPUT: init_string, | 
|  | 84 | +        } | 
|  | 85 | +    ) | 
|  | 86 | +    result = from_imagej().get() | 
|  | 87 | +    if result == ijserver.PYIMAGEJ_STATUS_STARTUP_FAILED: | 
|  | 88 | +        _shutdown_imagej() | 
|  | 89 | +        # Wait for the server to shut down | 
|  | 90 | +        while ijserver.is_server_running(): | 
|  | 91 | +            pass | 
|  | 92 | +        return False | 
|  | 93 | + | 
|  | 94 | +    global _init_method | 
|  | 95 | +    _init_method = init_string | 
|  | 96 | +    return True | 
|  | 97 | + | 
|  | 98 | + | 
|  | 99 | +def _manager() -> QueueManager: | 
|  | 100 | +    """ | 
|  | 101 | +    Helper method to return a QueueManager connected to the ImageJ server | 
|  | 102 | +    """ | 
|  | 103 | +    if not ijserver.is_server_running(): | 
|  | 104 | +        raise RuntimeError("No ImageJ server instance available") | 
|  | 105 | + | 
|  | 106 | +    manager = QueueManager( | 
|  | 107 | +        address=("127.0.0.1", ijserver.SERVER_PORT), authkey=ijserver._SERVER_KEY | 
|  | 108 | +    ) | 
|  | 109 | +    manager.connect() | 
|  | 110 | +    return manager | 
|  | 111 | + | 
|  | 112 | + | 
|  | 113 | +def _shutdown_imagej(): | 
|  | 114 | +    """ | 
|  | 115 | +    Helper method to send the shutdown signal to ImageJ. Intended to be called | 
|  | 116 | +    at process exit. | 
|  | 117 | +    """ | 
|  | 118 | +    if ijserver.is_server_running(): | 
|  | 119 | +        to_imagej().put({ijserver.PYIMAGEJ_KEY_COMMAND: ijserver.PYIMAGEJ_CMD_EXIT}) | 
|  | 120 | + | 
|  | 121 | + | 
|  | 122 | +def start_imagej_server(): | 
|  | 123 | +    """ | 
|  | 124 | +    If the ImageJ server is not already running, spawns the server in a new | 
|  | 125 | +    Process. Blocks until the server is up and running. | 
|  | 126 | +    """ | 
|  | 127 | +    if ijserver.is_server_running(): | 
|  | 128 | +        return | 
|  | 129 | + | 
|  | 130 | +    ctx = mp.get_context("spawn") | 
|  | 131 | +    p = ctx.Process(target=ijserver.main) | 
|  | 132 | +    p.start() | 
|  | 133 | + | 
|  | 134 | +    # wait for the server to start up | 
|  | 135 | +    ijserver.wait_for_server_startup() | 
|  | 136 | + | 
|  | 137 | +    # Ensure server shuts down when main app closes | 
|  | 138 | +    atexit.register(_shutdown_imagej) | 
0 commit comments