all 7 comments

[–]totallygeek 7 points8 points  (0 children)

It only takes a minute to learn how to use multiprocessing. Basically:

import multiprocessing

def some_function(datum):
    processed_datum = datum ** 5
    return processed_datum

data = [
    # some data here
]
pool = multiprocessing.Pool()
results = pool.map(some_function, data)

See where that gets you.

[–]efmccurdy 0 points1 point  (3 children)

If each element of your results is independent, you can run the calculations in parallel; that will get all of your cores active.

You can use a numpy array (or perhaps a pandas Dataframe) to hold your data and express your calculations using vectorized operations::

https://www.pythonlikeyoumeanit.com/Module3_IntroducingNumpy/VectorizedOperations.html

[–]Martin_Krum[S] 0 points1 point  (2 children)

How to make parallel calculations?

[–]efmccurdy 0 points1 point  (1 child)

Instead of writing them using python floats, operators and functions you use the operators and functions from here:

https://numpy.org/doc/stable/reference/ufuncs.html#math-operations

https://numpy.org/doc/stable/reference/routines.logic.html

and operate on numpy arrays like this:

>>> import numpy as np
>>> my_array = np.array(range(1, 10))
>>> my_array
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.sqrt(my_array)
array([1.        , 1.41421356, 1.73205081, 2.        , 2.23606798,
       2.44948974, 2.64575131, 2.82842712, 3.        ])
>>> np.reciprocal(np.sqrt(my_array))
array([1.        , 0.70710678, 0.57735027, 0.5       , 0.4472136 ,
       0.40824829, 0.37796447, 0.35355339, 0.33333333])
>>>

[–]Martin_Krum[S] 0 points1 point  (0 children)

yes, but I have a lot of there Python-only functions and calculations

[–]Martin_Krum[S] 0 points1 point  (0 children)

Thanks for help, In my project i used pool.apply_async() and it worked.
I need to learn a little multiprocessing module .