1import threading 2import time 3 4def fibonacci(n): 5 if n < 2: 6 return n 7 else: 8 return fibonacci(n-2) + fibonacci(n-1) 9 10# calculate fibonacci sequences four times in parallel 11 12start_time, threads = time.time(), [] 13 14for i in range(4): 15 t = threading.Thread(target = fibonacci, args = (35,)) 16 t.setDaemon(True) 17 t.start() 18 19 threads.append(t) 20 21for t in threads: 22 t.join() 23 24print('took %0.1f seconds' % (time.time() - start_time)) 25