-
Notifications
You must be signed in to change notification settings - Fork 18
Add async_local backend and allow using an existing dview for local and async_local backends #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ethanbb
wants to merge
13
commits into
nel-lab:master
Choose a base branch
from
proektlab:async-local
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a335825
Add HDF5 format movie reader
ethanbb a868ec3
Correctly set var_name_hdf5 when loading hdf5 and this param is set
ethanbb 3e75737
Merge branch 'master' of https://github.com/nel-lab/mesmerize-core
ethanbb 8e0a2d2
Merge branch 'master' of https://github.com/nel-lab/mesmerize-core
ethanbb b6d06d5
Merge branch 'master' of https://github.com/nel-lab/mesmerize-core
ethanbb 262cef0
Add async run method and dview argument to algos
ethanbb 020d29c
Don't close dview if passed in; add context manager to help
ethanbb d907ac6
switch from using asyncio to concurrent.futures and add test
ethanbb f71bb01
Make sure all Waitables throw exception on failure
ethanbb 1e8e36f
Check type of setup_cluster return value
ethanbb dc44d17
Make dview compatible with other cluster types (conforming to protocol)
ethanbb bd4c63d
Fix CustomCluster protocol to work w/ futures again
ethanbb 5cafeea
Change definition of cluster again
ethanbb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from contextlib import contextmanager | ||
import os | ||
import psutil | ||
from typing import (Optional, Union, Generator, Protocol, | ||
Callable, TypeVar, Sequence, Iterable, runtime_checkable) | ||
|
||
import caiman as cm | ||
from caiman.cluster import setup_cluster | ||
from ipyparallel import DirectView | ||
from multiprocessing.pool import Pool | ||
|
||
|
||
RetVal = TypeVar("RetVal") | ||
@runtime_checkable | ||
class CustomCluster(Protocol): | ||
""" | ||
Protocol for a cluster that is not a multiprocessing pool | ||
(including ipyparallel.DirectView) | ||
""" | ||
|
||
def map_sync( | ||
self, fn: Callable[..., RetVal], args: Iterable | ||
) -> Sequence[RetVal]: ... | ||
|
||
def __len__(self) -> int: | ||
"""return number of workers""" | ||
... | ||
|
||
|
||
Cluster = Union[Pool, CustomCluster, DirectView] | ||
|
||
|
||
def get_n_processes(dview: Optional[Cluster]) -> int: | ||
"""Infer number of processes in a multiprocessing or ipyparallel cluster""" | ||
if isinstance(dview, Pool): | ||
assert hasattr(dview, '_processes'), "Pool not keeping track of # of processes?" | ||
return dview._processes # type: ignore | ||
elif dview is not None: | ||
return len(dview) | ||
else: | ||
return 1 | ||
|
||
|
||
@contextmanager | ||
def ensure_server(dview: Optional[Cluster]) -> Generator[tuple[Cluster, int], None, None]: | ||
""" | ||
Context manager that passes through an existing 'dview' or | ||
opens up a multiprocessing server if none is passed in. | ||
If a server was opened, closes it upon exit. | ||
Usage: `with ensure_server(dview) as (dview, n_processes):` | ||
""" | ||
if dview is not None: | ||
yield dview, get_n_processes(dview) | ||
else: | ||
# no cluster passed in, so open one | ||
procs_available = psutil.cpu_count() | ||
if procs_available is None: | ||
raise RuntimeError('Cannot determine number of processes') | ||
|
||
if "MESMERIZE_N_PROCESSES" in os.environ.keys(): | ||
try: | ||
n_processes = int(os.environ["MESMERIZE_N_PROCESSES"]) | ||
except: | ||
n_processes = procs_available - 1 | ||
else: | ||
n_processes = procs_available - 1 | ||
|
||
# Start cluster for parallel processing | ||
_, dview, n_processes = setup_cluster( | ||
backend="multiprocessing", n_processes=n_processes, single_thread=False | ||
) | ||
assert isinstance(dview, Pool) and isinstance(n_processes, int), 'setup_cluster with multiprocessing did not return a Pool' | ||
try: | ||
yield dview, n_processes | ||
finally: | ||
cm.stop_server(dview=dview) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure what this part is for; it doesn't really work with the changes here, so I just deleted it, but we can try to do something else if it's important.