Skip to content

Commit e0feb61

Browse files
committed
Single Dask client is enough
1 parent a5047b2 commit e0feb61

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

src/moldrug/runner.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
try:
88
from dask.distributed import Client
9-
from dask_jobqueue import JobQueueCluster
109

1110
dask_available = True
1211
except ImportError:
@@ -26,15 +25,10 @@ def parallel_execution_multiprocessing(func: Callable, entries: List, process_co
2625

2726

2827
if dask_available:
29-
def parallel_execution_dask_local(func: Callable, entries: List, process_count: int, process_threads_count: int):
30-
client = Client(n_workers=process_count, threads_per_worker=process_threads_count)
31-
28+
def parallel_execution_dask_local(func: Callable, entries: List, client: Client):
3229
return client.gather(client.map(func, entries))
3330

34-
35-
def parallel_execution_dask_cluster(func: Callable, entries: List, cluster: JobQueueCluster):
36-
client = Client(cluster)
37-
31+
def parallel_execution_dask_cluster(func: Callable, entries: List, client: Client):
3832
return client.gather(client.map(func, entries))
3933

4034

@@ -50,10 +44,11 @@ class Runner:
5044
thread_count: Optional[int]
5145
process_count: Optional[int]
5246
dask_cluster = None
47+
client = None
5348

5449
def __init__(self, mode: RunnerMode, thread_count: Optional[int] = None, process_count: Optional[int] = None, dask_cluster = None):
5550
if mode in [RunnerMode.DASK_LOCAL, RunnerMode.DASK_JOB_QUEUE_CLUSTER]:
56-
assert dask_cluster is not None, "Execution using Dask requires installation of optional dependencies. The optional pip package group is called 'cluster'"
51+
assert dask_available is not None, "Execution using Dask requires installation of optional dependencies. The optional pip package group is called 'cluster'"
5752

5853
if mode is RunnerMode.SERIAL:
5954
assert thread_count is None and process_count is None and dask_cluster is None, "Serial execution doesn't take any parameters."
@@ -62,7 +57,7 @@ def __init__(self, mode: RunnerMode, thread_count: Optional[int] = None, process
6257
elif mode in [RunnerMode.DASK_LOCAL]:
6358
assert (thread_count is not None or process_count is not None) and dask_cluster is None, "Only process count and/or thread count are needed."
6459
elif mode in [RunnerMode.DASK_JOB_QUEUE_CLUSTER]:
65-
assert thread_count is None and process_count is None and dask_cluster is not None, "Dask cluster execution takes only a Dask cluster object."
60+
assert thread_count is None and process_count is None and dask_cluster is not None, "Dask execution takes only a Dask cluster object."
6661
else:
6762
assert False
6863

@@ -71,14 +66,20 @@ def __init__(self, mode: RunnerMode, thread_count: Optional[int] = None, process
7166
self.process_count = process_count
7267
self.cluster = dask_cluster
7368

69+
if mode in [RunnerMode.DASK_LOCAL]:
70+
self.client = Client(n_workers=process_count, threads_per_worker=thread_count)
71+
72+
if mode in [RunnerMode.DASK_JOB_QUEUE_CLUSTER]:
73+
self.client = Client(dask_cluster)
74+
7475
def run(self, func: Callable, entries: List):
7576
if self.mode == RunnerMode.SERIAL:
7677
return serial_execution(func, entries)
7778
elif self.mode == RunnerMode.MULTIPROCESSING:
7879
return parallel_execution_multiprocessing(func, entries, process_count=self.process_count)
7980
elif self.mode == RunnerMode.DASK_LOCAL:
80-
return parallel_execution_dask_local(func, entries, process_count=self.process_count, process_threads_count=self.thread_count)
81+
return parallel_execution_dask_local(func, entries, self.client)
8182
if self.mode == RunnerMode.DASK_JOB_QUEUE_CLUSTER:
82-
return parallel_execution_dask_cluster(func, entries, cluster=self.cluster)
83+
return parallel_execution_dask_cluster(func, entries, self.client)
8384
else:
8485
assert False

0 commit comments

Comments
 (0)