Python Python code using Process class

AI Thread Summary
The code provided aims to use multiprocessing to double each element in a list and return the results. However, the current implementation results in a list of `None` values because the return values from the `func` are not captured correctly. The `join()` method does not return the function's output; it only waits for the process to finish. To achieve the desired output of `[2, 4, 6, 8, 10]`, the code needs to be modified to store the results in a way that allows capturing the output of each process. One common approach is to use a `multiprocessing.Queue` to collect the results from each process, ensuring that the output can be accessed after all processes complete.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
9
Views
3K
Replies
16
Views
2K
Replies
29
Views
3K
Replies
11
Views
1K
Replies
15
Views
2K
Replies
8
Views
1K
Back
Top