Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combining data from multiple processes

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Write functions that return the result (like a math problem y = f(x)). Then map() will take all the processes and return them as a list.

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])


def calculate_output(work_data):
    work_output=work_data[1]*work_data[2]
    print(f"Process {work_data[0]} {work_data[1]}*{work_data[2]}={work_output}\n")
    return work_output

def work_log(work_data):
    print(F"Process {work_data[0]} started\n")
    return calculate_output(work_data)
    print(f"Process {work_data[0]} Finished.\n")

def pool_handler():
    p=Pool(4)
    results = p.map(work_log, work)
    print(results)

if __name__ == '__main__':
    pool_handler()

stdout:

Process process1 started

Process process1 1*2=2

Process process2 started

Process process2 2*3=6

Process process4 started

Process process4 4*5=20

Process process5 started

Process process5 5*6=30

Process process6 started

Process process6 6*7=42

Process process3 started

Process process3 3*4=12

[2, 6, 12, 20, 30, 42]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading