I'm learning about threading library in Python. I don't understand, how to run two threads in parallel?
Here are my python programs:
Program without threading (fibsimple.py)
def fib(n):
if n < 2:
return n
else:
return fib(n-1) + fib(n-2)
fib(35)
fib(35)
print "Done"
Running time:
$ time python fibsimple.py
Done
real 0m7.935s
user 0m7.922s
sys 0m0.008s
Same program with threading(fibthread.py)
from threading import Thread
def fib(n):
if n < 2:
return n
else:
return fib(n-1) + fib(n-2)
t1 = Thread(target = fib, args = (35, ))
t1.start()
t2 = Thread(target = fib, args = (35, ))
t2.start()
t1.join()
t2.join()
print "Done"
Running time:
$ time python fibthread.py
Done
real 0m12.313s
user 0m10.894s
sys 0m5.043s
I don't understand why thread program is taking more time? It should be almost half, if threads are running in parallel.
But If I implement the same program with multiprocessing library, time will become half.
program with multiprocess(fibmultiprocess.py)
from multiprocessing import Process
def fib(n):
if n < 2:
return n
else:
return fib(n-1) + fib(n-2)
p1 = Process(target = fib, args = (35, ))
p1.start()
p2 = Process(target = fib, args = (35, ))
p2.start()
p1.join()
p2.join()
print "Done"
Running time
$ time python fibmultiporcess.py
Done
real 0m4.303s
user 0m8.065s
sys 0m0.007s
Can someone explain, How to run threads in parallel? How multiprocessing and thread-parallelism are different? Any help would be appreciated.