Python 中的 Dask 并行编程
James Fulton
Climate Informatics Researcher
dask.delayed() 创建的延迟管道# 使用默认 result = x.compute()result = dask.compute(x)# 使用线程 result = x.compute(scheduler='threads')result = dask.compute(x, scheduler='threads')# 使用进程 result = x.compute(scheduler='processes')result = dask.compute(x, scheduler='processes')
from dask.distributed import LocalCluster
cluster = LocalCluster(
processes=True,
n_workers=2,
threads_per_worker=2
)
print(cluster)
LocalCluster(..., workers=2, threads=4, memory=31.38 GiB)
from dask.distributed import LocalCluster
cluster = LocalCluster(
processes=False,
n_workers=2,
threads_per_worker=2
)
print(cluster)
LocalCluster(..., workers=2, threads=4, memory=31.38 GiB)
cluster = LocalCluster(processes=True)
print(cluster)
LocalCluster(..., workers=4 threads=8, memory=31.38 GiB)
cluster = LocalCluster(processes=False)
print(cluster)
LocalCluster(..., workers=1 threads=8, memory=31.38 GiB)
from dask.distributed import Client, LocalCluster
cluster = LocalCluster(
processes=True,
n_workers=4,
threads_per_worker=2
)
client = Client(cluster)
print(client)
<Client: 'tcp://127.0.0.1:61391' processes=4 threads=8, memory=31.38 GiB>
先建集群,再传给客户端
cluster = LocalCluster(
processes=True,
n_workers=4,
threads_per_worker=2
)
client = Client(cluster)
print(client)
<Client: ... processes=4 threads=8, ...>
直接建客户端(其会创建集群)
client = Client(
processes=True,
n_workers=4,
threads_per_worker=2
)
print(client)
<Client: ... processes=4 threads=8, ...>
client = Client(processes=True) # 默认使用该客户端 result = x.compute()# 仍可切换到其他调度器 result = x.compute(scheduler='threads')# 也可显式用客户端 result = client.compute(x)
LocalCluster():在本机上创建集群。Python 中的 Dask 并行编程