Sometimes we want to run a method using multiple processors to process our code due to a costly function. Below is an example of how you could do it. There is other api’s you could use like ‘map’ but here is just one example.
- from multiprocessing import Pool
- # Sets the pool to utilize 4 processes
- pool = Pool(processes=4)
- result = pool.apply_async(func=my_method, args=("some_info",))
- # Performs the aync function
- data = result.get()
- pool.close()