Python code using Process class

Click For Summary
SUMMARY

The discussion centers on a Python code snippet utilizing the multiprocessing module to achieve parallel processing. The user aims to return a list of doubled values from the input list [1,2,3,4,5] but encounters an issue where the output is [None, None, None, None, None]. The solution involves modifying the argument passed to the Process class and ensuring that the results from each process are correctly captured. The corrected code successfully returns the desired output of [2,4,6,8,10].

PREREQUISITES
  • Understanding of Python programming
  • Familiarity with the multiprocessing module
  • Knowledge of function return values in Python
  • Basic concepts of parallel processing
NEXT STEPS
  • Explore advanced features of the multiprocessing module
  • Learn about Queue and Pipe for inter-process communication
  • Investigate error handling in multiprocessing applications
  • Study performance optimization techniques for parallel processing in Python
USEFUL FOR

Python developers, software engineers, and anyone interested in implementing parallel processing to enhance performance in their applications.

fred_91
Messages
38
Reaction score
0

Homework Statement



I have the following code (please see below). I want it to return [2,4,6,8,10] which is the function evaluated at the given list [1,2,3,4,5]. I want to do this using the process command.

My problem is:
print A returns
[None, None, None, None, None]
But I want it to return
[2,4,6,8,10]

Many thanks in advance.

Homework Equations

The Attempt at a Solution



import multiprocessing as mp
from multiprocessing import Process

def func(x):
y = x*2
print y
return y

def OutFunc(f1,a):
if __name__ == '__main__':
OutputList = []
res = []
for I in range(5):
p=mp.Process(target=f1, args = (a,))
OutputList.append(p)
p.start()

for proc in OutputList:
result = proc.join()
res.append(result)
return res

A = OutFunc(func,[1,2,3,4,5])
print A
 
Technology news on Phys.org
When you post code, please add a [ code ] tag at the top and a [ /code ] tag at the bottom (no extra spaces though). Doing so preserves your indentation, which is crucial in Python code. I have added these tags to your code.
I have also changed your loop variable from i to j, because i in brackets is interpreted by the forum software as the start of an italics tag.
fred_91 said:

Homework Statement



I have the following code (please see below). I want it to return [2,4,6,8,10] which is the function evaluated at the given list [1,2,3,4,5]. I want to do this using the process command.

My problem is:
print A returns
[None, None, None, None, None]
But I want it to return
[2,4,6,8,10]

Many thanks in advance.

Homework Equations

The Attempt at a Solution


Python:
import multiprocessing as mp
from multiprocessing import Process

def func(x):
   y = x*2
   print y
   return y

def OutFunc(f1,a):
    if __name__ == '__main__':
        OutputList = []
        res = []
        for j in range(5):
           p=mp.Process(target=f1, args = (a[j],))
           OutputList.append(p)
           p.start()
         
        for proc in OutputList:
            result = proc.join()
            res.append(result)
    return res

A = OutFunc(func,[1,2,3,4,5])
print A
 
Thank you. I hope it looks clearer now.

Any help would be very much appreciated.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K