Použití procesů a vláken

Parallel Programming with Dask in Python

James Fulton

Climate Informatics Researcher

Výchozí plánovač Dasku

Vlákna

  • Dask arrays
  • Dask DataFrames
  • Zpožděné pipeline vytvořené pomocí dask.delayed()

Procesy

  • Dask bags
Parallel Programming with Dask in Python

Výběr plánovače

# Use default
result = x.compute()

result = dask.compute(x)
# Use threads result = x.compute(scheduler='threads')
result = dask.compute(x, scheduler='threads')
# Use processes result = x.compute(scheduler='processes')
result = dask.compute(x, scheduler='processes')
Parallel Programming with Dask in Python

Rekapitulace – vlákna vs. procesy

Vlákna

  • Rychlé spuštění
  • Není třeba přenášet data
  • Omezena GIL – v daný okamžik může kód číst pouze jedno vlákno

Procesy

  • Pomalé spuštění
  • Pomalý přenos dat
  • Každý má vlastní GIL – nemusí se střídat při čtení kódu
Parallel Programming with Dask in Python

Vytvoření lokálního clusteru

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)
Parallel Programming with Dask in Python

Vytvoření lokálního clusteru

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)
Parallel Programming with Dask in Python

Jednoduchý lokální cluster

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)
Parallel Programming with Dask in Python

Vytvoření klienta

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>
Parallel Programming with Dask in Python

Snadné vytvoření klienta

Vytvoření clusteru a jeho předání klientovi

cluster = LocalCluster(
    processes=True, 
    n_workers=4,
    threads_per_worker=2
)

client = Client(cluster)

print(client)
<Client: ... processes=4 threads=8, ...>

Vytvoření klienta, který si vytvoří vlastní cluster

client = Client(
    processes=True, 
    n_workers=4,
    threads_per_worker=2
)



print(client)
<Client: ... processes=4 threads=8, ...>
Parallel Programming with Dask in Python

Použití clusteru

client = Client(processes=True)

# Default uses the client
result = x.compute()

# Can still change to other schedulers result = x.compute(scheduler='threads')
# Can explicitly use client result = client.compute(x)
Parallel Programming with Dask in Python

Další typy clusterů

  • LocalCluster() – cluster na vašem počítači.
  • Ostatní typy clusterů rozdělují výpočty mezi různé počítače
Parallel Programming with Dask in Python

Pojďme si procvičit!

Parallel Programming with Dask in Python

Preparing Video For Download...