jobmanager/examples/pool_map.py

40 lines
824 B
Python
Raw Normal View History

2015-05-07 13:39:51 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function
from os.path import split, dirname, abspath
import sys
import time
import numpy as np
import multiprocessing as mp
# Add parent directory to beginning of path variable
sys.path.insert(0, dirname(dirname(abspath(__file__))))
import jobmanager
2015-11-20 00:13:19 +01:00
def func(x, y, z):
2015-05-07 13:39:51 +02:00
"""Example function with only one argument"""
2015-11-20 00:13:19 +01:00
time.sleep(x/10)
return np.sum([x, y, z])
def wrapper(data):
return func(*data)
2015-05-07 13:39:51 +02:00
# Create list of parameters
a = list()
for i in range(10):
2015-11-20 00:13:19 +01:00
a.append([i, 2.34, 9])
2015-05-07 13:39:51 +02:00
2015-11-11 15:55:11 +01:00
# mp.Pool example:
p_mp = mp.Pool()
2015-11-20 00:13:19 +01:00
res_mp = p_mp.map(wrapper, a)
2015-05-07 13:39:51 +02:00
2015-11-11 15:55:11 +01:00
# equivalent to mp.Pool() but with progress bar:
p_jm = jobmanager.decorators.Pool()
2015-11-20 00:13:19 +01:00
res_jm = p_jm.map(wrapper, a)
2015-05-07 13:39:51 +02:00
2015-11-11 15:55:11 +01:00
assert res_mp == res_jm
print("result: ", res_jm)