Sử dụng process và thread

Lập trình song song với Dask trong Python

James Fulton

Climate Informatics Researcher

Bộ lập lịch mặc định của Dask

Thread

  • Dask Array
  • Dask DataFrame
  • Pipeline trì hoãn với dask.delayed()

Process

  • Dask Bag
Lập trình song song với Dask trong Python

Chọn bộ lập lịch (scheduler)

# Dùng mặc định
result = x.compute()

result = dask.compute(x)
# Dùng thread result = x.compute(scheduler='threads')
result = dask.compute(x, scheduler='threads')
# Dùng process result = x.compute(scheduler='processes')
result = dask.compute(x, scheduler='processes')
Lập trình song song với Dask trong Python

Tóm tắt - thread vs. process

Thread

  • Khởi tạo rất nhanh
  • Không cần chuyển dữ liệu sang thread
  • Bị giới hạn bởi GIL: chỉ một thread đọc mã tại một thời điểm

Process

  • Mất thời gian khởi tạo
  • Truyền dữ liệu chậm
  • Mỗi process có GIL riêng nên không cần thay phiên đọc mã
Lập trình song song với Dask trong Python

Tạo cụm cục bộ (local cluster)

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)
Lập trình song song với Dask trong Python

Tạo cụm cục bộ (local cluster)

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)
Lập trình song song với Dask trong Python

Cụm cục bộ đơn giản

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)
Lập trình song song với Dask trong Python

Tạo client

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>
Lập trình song song với Dask trong Python

Tạo client nhanh chóng

Tạo cluster rồi truyền vào client

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

client = Client(cluster)

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

Tạo client để tự tạo cluster

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



print(client)
<Client: ... processes=4 threads=8, ...>
Lập trình song song với Dask trong Python

Dùng cụm (cluster)

client = Client(processes=True)

# Mặc định dùng client
result = x.compute()

# Vẫn có thể đổi sang bộ lập lịch khác result = x.compute(scheduler='threads')
# Có thể gọi client rõ ràng result = client.compute(x)
Lập trình song song với Dask trong Python

Các loại cụm khác

  • LocalCluster() - Cụm trên máy của bạn.
  • Các loại cụm khác phân tán tính toán qua nhiều máy
Lập trình song song với Dask trong Python

¡Vamos a practicar!

Lập trình song song với Dask trong Python

Preparing Video For Download...