python_multiprocessing
This is an old revision of the document!
using pool
Goal: Run two functions in parallel. The second function should start irrespective of whether the first one finished or not.
$ cat use_pool.py from multiprocessing import Pool from time import sleep def square(x): res = x*x print('square(', x, ') = ', res) # artificially increase the runtime of the function sleep(10) return res def cube(x): res = x**3 print('cube(', x, ') = ', res) # artificially increase the runtime of the function sleep(10) return res if __name__ == '__main__': num_workers = 2 pool = Pool(num_workers) funcs = [square, cube] token = 2 tasks = [pool.apply_async(func, (token,)) for func in funcs] [task.get() for task in tasks]
$ python use_pool.py square( 2 ) = 4 cube( 2 ) = 8
$ python --version Python 3.9.4
links I came across
python_multiprocessing.1626896625.txt.gz · Last modified: 2021/07/21 19:43 by raju