For this multithreaded python code, I'd like to combine the data from each process variable "work_output" into a global variable "workOutput". I'm not sure how to access variables outside individual processes or how to combine the data from multiple processes.
#edited code based on comment that answered the question
from multiprocessing import Pool, cpu_count
import time
import psutil
import os
import math
work =(["process1", 1,2], ["process2", 2,3], ["process3", 3,4], ["process4", 4,5], ["process5", 5,6], ["process6", 6,7])
workOutputAll=[]
def calculateOutput(work_data):
#timeToSleep=int(100)
#time.sleep(timeToSleep)
work_output=work_data[1]*work_data[2]
print(f"Process {work_data[0]} {work_data[1]}*{work_data[2]}={work_output}\n")
#workOutput.append(work_output)
return work_output
def work_log(work_data):
print(F"Process {work_data[0]} started\n")
y=calculateOutput(work_data)
print(f"Process {work_data[0]} Finished.\n")
return y
def pool_handler():
p=Pool(4)
z=p.map(work_log, work)
workOutputAll.append(z)
if __name__ == '__main__':
pool_handler()
print(workOutputAll)