Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use with a real function? #1

Open
daviddoria opened this issue Apr 7, 2015 · 1 comment
Open

How to use with a real function? #1

daviddoria opened this issue Apr 7, 2015 · 1 comment

Comments

@daviddoria
Copy link

This is nice, but with a non-toy function, you would want to pass some actual data to every call of test(), not just the location to display the progress bar, right? As I understand it you can only pass one variable thing (and as many constant things you want via a Partial) to the map() calls.

@aaren
Copy link
Owner

aaren commented Apr 7, 2015

You can pass a single list or dictionary containing whatever data you want.

def work_function(data):
    time.sleep(data)

def test_function(payload):
    writer = Writer(payload['location'])
    pbar = ProgressBar(fd=writer)
    pbar.start()
    for i in range(100):
        # mimic doing some stuff
        work_function(payload['data'])
        pbar.update(i)
    pbar.finish()
locations = [(0, 1), (0, 6), (0, 7)]
data = [0.01, 0.02, 0.03]

payload = [{'location': loc, 'data': d} for loc, d in zip(locations, data)]

p = Pool()
p.map(test_function, payload)
p.close()

If you want more control than Pool can give you then you could start up a number of Process instances.

https://docs.python.org/2/library/multiprocessing.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants