प्रोसेस और थ्रेड्स का उपयोग

Python में Dask के साथ Parallel Programming

James Fulton

Climate Informatics Researcher

Dask डिफॉल्ट शेड्यूलर

Threads

  • Dask arrays
  • Dask DataFrames
  • dask.delayed() से बनी delayed पाइपलाइन्स

Processes

  • Dask bags
Python में Dask के साथ Parallel Programming

शेड्यूलर चुनना

# डिफॉल्ट उपयोग करें
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')
Python में Dask के साथ Parallel Programming

रीकैप - threads बनाम processes

Threads

  • शुरू होने में बहुत तेज
  • इनमें डेटा ट्रांसफर करने की ज़रूरत नहीं
  • GIL से सीमित, जो एक समय में एक थ्रेड को कोड पढ़ने देता है

Processes

  • सेट अप में समय लगता है
  • इनमें डेटा ट्रांसफर धीमा है
  • हर प्रोसेस का अपना GIL होता है, इसलिए बारी-बारी से कोड पढ़ने की ज़रूरत नहीं
Python में Dask के साथ Parallel Programming

लोकल क्लस्टर बनाना

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)
Python में Dask के साथ Parallel Programming

लोकल क्लस्टर बनाना

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)
Python में Dask के साथ Parallel Programming

सिंपल लोकल क्लस्टर

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)
Python में Dask के साथ Parallel Programming

क्लायंट बनाना

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>
Python में Dask के साथ Parallel Programming

क्लायंट आसानी से बनाना

क्लस्टर बनाएँ और उसे क्लायंट में पास करें

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, ...>
Python में Dask के साथ Parallel Programming

क्लस्टर का उपयोग

client = Client(processes=True)

# डिफॉल्ट क्लायंट ही उपयोग होता है
result = x.compute()

# फिर भी अन्य शेड्यूलर चुन सकते हैं result = x.compute(scheduler='threads')
# क्लायंट को स्पष्ट रूप से उपयोग कर सकते हैं result = client.compute(x)
Python में Dask के साथ Parallel Programming

क्लस्टर के अन्य प्रकार

  • LocalCluster() - आपके कंप्यूटर पर क्लस्टर.
  • अन्य क्लस्टर प्रकार computation को अलग-अलग कंप्यूटरों में बाँटते हैं
Python में Dask के साथ Parallel Programming

अभ्यास करते हैं!

Python में Dask के साथ Parallel Programming

Preparing Video For Download...