diff --git a/xarray_tutorial/parallelize.py b/xarray_tutorial/parallelize.py new file mode 100644 index 0000000..7576a69 --- /dev/null +++ b/xarray_tutorial/parallelize.py @@ -0,0 +1,28 @@ +import time +import multiprocessing + +def basic_func(x): + if x == 0: + return 'zero' + elif x%2 == 0: + return 'even' + else: + return 'odd' + +def multiprocessing_func(x): + y = x*x + time.sleep(2) + print('{} squared results in a/an {} number'.format(x, basic_func(y))) + +if __name__ == '__main__': + starttime = time.time() + processes = [] + for i in range(0,10): + p = multiprocessing.Process(target=multiprocessing_func, args=(i,)) + processes.append(p) + p.start() + + for process in processes: + process.join() + + print('That took {} seconds'.format(time.time() - starttime))