Joblib

Install

`pip install joblib`

การหา square root ด้วย Python

```from math import sqrt
li = [sqrt(i ** 2) for i in range(10)]
print(li)
# [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]```

ใช้ joblib

```from math import sqrt
from joblib import Parallel, delayed
li = Parallel(n_jobs=2)(delayed(sqrt)(i**2) for i in range(10))
print(li)
# [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]```

verbose: int, optional

The verbosity level: if non zero, progress messages are printed. Above 50, the output is sent to stdout. The frequency of the messages increases with the verbosity level. If it more than 10, all iterations are reported.

```%python
from math import sqrt
from joblib import Parallel, delayed
li = Parallel(n_jobs=2, verbose=100)(delayed(sqrt)(i**2) for i in range(10))
print(li)

# [Parallel(n_jobs=2)]: Using backend LokyBackend with 2 concurrent workers.
# [Parallel(n_jobs=2)]: Done   1 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   2 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   3 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   4 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   5 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   6 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   7 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   8 out of  10 | elapsed:    0.5s remaining:    0.1s
# [Parallel(n_jobs=2)]: Done  10 out of  10 | elapsed:    0.5s remaining:    0.0s
# [Parallel(n_jobs=2)]: Done  10 out of  10 | elapsed:    0.5s finished
# [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]```

default `backend` เป็น `loky` เราสามารถเปลี่ยนเป็น `threading` , `multiprocessing` ได้

```from math import sqrt
from joblib import Parallel, delayed
li = Parallel(n_jobs=2, verbose=100, backend='threading')(delayed(sqrt)(i**2) for i in range(10))
print(li)```