Q:

Multithreading in Python in a recursive function

I have a function which recursively calls itself. Here is an example:
def f(a,b=0):
    if b < 5:
        for i in range(10):
            a += f(a+i,b+1)
    return a+b

print f(3)

Now I want to run 10 function calls inside the function each one in a separate thread simultaneously but get the return from all in one variable together.
Could someone lead me into the right direction?

A:

Try thinking more precisely about how you want the multithreading to work.  
The way you asked the question suggests that you want to spawn 10 threads for each recursive function call.  This means that after a single level of recursion, you'll have 100 threads, after 2 levels, you'll have 1000 threads, and so on.  This is probably not what you want unless you're trying to freeze up your OS.
Two alternatives include:

only spawn threads on the first call, but not on recursive calls, or
create a thread pool and use a work queue.

Another approach would be to think about the math that's being done by the function and analytically simplify it to avoid the looping and/or recursion.

