I have been trying to incorporate multithreading in my code which calculates the sum of the Fibonacci numbers whose index is given by a randomly generated number array. The array is of 10000 numbers. The problem is this code doesn't work for 10k numbers but sometimes works for 1000 numbers.
import random, threading
arr = []
def fibo(num, cache = {1:1, 2:1}):
    if num not in cache:
        cache[num] = fibo(num-1, cache) + fibo(num-2, cache)
    return cache[num]
def rand():
    global arr
    arr = [random.randrange(1, 21) for _ in range(10000)]
def func(arr):      
    fibo_arr = []
    fibo_arr  = [fibo(n) for n in arr]
    print(f'Sum: {sum(fibo_arr)}')
if __name__ == "__main__":
    x = threading.Thread(target=rand)
    x.start()  
    y = threading.Thread(target = func, args = (arr,))
    y.start()
    x.join() 
    y.join()
Why is the code only working for a specific range and that too with inconsistency? Is there a better way of doing this using synchronization?
