0

I have a function in python which I need to execute it multiple times and at the same time (to save computational cost). The following is a simple example and I don't know how to call my function at the same time!

def f(x):
    return x

y1=f(x1)
y2=f(x2)

I need to execute y1 and y2 at the same time and wait until both finish and save the results. Thank you all in advance.

1

2 Answers 2

2

Another solution that might help you:

import Queue
import threading

# your function with a slight change:
def f(q,x):
    q.put(x)

inputs = [1,2,3,4]

# aggregate to a queue:
q = Queue.Queue()

for x in inputs:
    t = threading.Thread(target=f, args=(q,x))
    t.daemon = True
    t.start()

outputs = q.get()
print outputs

output: 1

Here q holds the return value of f.

In any case I suggest you try and read this.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response. As I'm new to python, I don't know how to execute the file! As I understood, it should calls the f, 4 times in parallel and we should have 4 results in the output?
For every q.get() the return value is the output.
2

I would encourage you to read the https://docs.python.org/3/library/multiprocessing.html documentation, which has a great example. If you're using Python 3.x

from multiprocessing import Pool
def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))

If you're using python 2.7: https://docs.python.org/2.7/library/multiprocessing.html

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))

7 Comments

multiprocessing Pools are definitely the way to go, but PLEASE read all the warning notes in the docs!
Thank you very much for your response. I'm new to python and when I run this code in IDLE, I get this error, while I should print out [1,4,9] Traceback (most recent call last): File "C:/Users/mohammad/Desktop/r.py", line 8, in <module> with Pool(5) as p: AttributeError: exit
If you're using python 2.x (like python 2.7), use the instructions here: docs.python.org/2.7/library/multiprocessing.html I've added this to the answer.
Thank you very much @AbrahamB I'm wondering if number 5 shows the parallelization? all the 3 inputs will be executed at the same time? if one of them returns longer than the others, it doesnt crash?
@Mohammad, per: docs.python.org/2/library/…: processes is the number of worker processes to use.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.