1
import random
import threading
import multiprocessing as mp
import time

This is the global variable I want every process to change

result = 0
lock = mp.Lock()

Function I used to test the multiprocessing performance

def list_append(count, id, out_list):

    #lock.acquire()
    global result
    result = result+1
    #lock.release()
    for i in range(count):
        out_list.append(random.random())

Using multiprocessing I have a good performance but I am not able to change the global variable.

start = time.time()
if __name__ == "__main__":
    size = 10000000   # Number of random numbers to add
    threads = 6   # Number of threads to create
    # Create a list of jobs and then iterate through
    # the number of threads appending each thread to
    # the job list
    #global result
    jobs = []
    for i in range(0, threads):
        out_list = list()
        process = mp.Process(target=list_append,args=(size, i, out_list))
        jobs.append(process)
    # Start the threads (i.e. calculate the random number lists)
    for j in jobs:
            j.start()

    # Ensure all of the threads have finished
    for j in jobs:
        j.join()

    print ("List processing complete.")
    end = time.time()
    print(end - start)
    print(result)

Using threading, I am able to change the global variable value but with poor performance.

start = time.time()
if __name__ == "__main__":
    size = 10000000   # Number of random numbers to add
    threads = 6   # Number of threads to create

# Create a list of jobs and then iterate through
# the number of threads appending each thread to
# the job list
    jobs = []
    for i in range(0, threads):
        out_list = list()
        thread = threading.Thread(target=list_append(size, i, out_list))
        jobs.append(thread)
# Start the threads (i.e. calculate the random number lists)
    for j in jobs:
        j.start()

# Ensure all of the threads have finished
    for j in jobs:
        j.join()

    print ("List processing complete.")
    end = time.time()
    print(end - start)
    print(result)

I would like to know how I can change the global variable value using multiprocessing.

Wesley LB
  • 11
  • 1

0 Answers0