Python code using Process class

  • Comp Sci
  • Thread starter fred_91
  • Start date
  • #1
39
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
 

Answers and Replies

  • #2
34,182
5,799
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.

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
 
  • #3
39
0
Thank you. I hope it looks clearer now.

Any help would be very much appreciated.
 

Related Threads on Python code using Process class

  • Last Post
Replies
4
Views
1K
  • Last Post
Replies
1
Views
1K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
0
Views
1K
Replies
4
Views
3K
Replies
1
Views
2K
Replies
6
Views
745
Replies
2
Views
537
Top