parallel program in python using Threads
Question: Generating the sum from adding integer numbers successively up to n where n =
2000 given by the following formula: n(n+1)/2 so far i have don it in serial.I
need help on how to make it compute in parallel such that it adaptively make
use of all the available processors/cores on the host computer.

    
    
    #!/usr/bin/env python3
    from datetime import datetime
    n=1
    v=0
    start_time = datetime.now() 
    while n<=10:
    
     (n*(n+1)/2)
     b=(n*(n+1)/2)
     n = n+1
    
    end_time =datetime.now()
    
    print (b)
    
     print('Time taken : {}'. format(end_time-start_time))
    


Answer: To do this, you need to use `multiprocessing`, which lets you create processes
and assign procedures to them. Here's a code snippet that does part of what
you want:

    
    
    #!/usr/bin/env python3
    
    from datetime import datetime
    
    MAX_NUM = 10000000
    NUMPROCS = 1
    
    # LINEAR VERSION
    start_time = datetime.now() 
    my_sum = 0
    counter = 1
    while counter <= MAX_NUM:
        my_sum += counter
        counter += 1
    end_time =datetime.now()
    
    print (my_sum)
    print('Time taken : {}'. format(end_time-start_time))
    
    # THREADING VERSION
    from multiprocessing import Process, Queue
    
    start_time = datetime.now() 
    
    def sum_range(start,stop,out_q):
        i = start
        counter = 0
        while i < stop:
            counter += i
            i += 1
        out_q.put(counter)
    
    mysums = Queue()
    mybounds = [1+i for i in range(0,MAX_NUM+1,int(MAX_NUM/NUMPROCS))]
    myprocs = []
    
    for i in range(NUMPROCS):
        p = Process(target=sum_range, args=(mybounds[i],mybounds[i+1],mysums))
        p.start()
        myprocs.append(p)
    
    mytotal = 0
    for i in range(NUMPROCS):
        mytotal += mysums.get()
    
    for i in range(NUMPROCS):
        myprocs[i].join()
    
    print(mytotal)
    end_time =datetime.now()
    print('Time taken : {}'. format(end_time-start_time))
    

Although the code doesn't adaptively use processors, it does divide the task
into a prespecified number of processes.

