Python code using Process class

In summary, the conversation is about a code that is supposed to return [2,4,6,8,10] when evaluated at the given list [1,2,3,4,5] using the process command. However, the problem is that the code returns [None, None, None, None, None] instead. The attempt at a solution involves using multiprocessing and creating a list of processes to execute the function on each element of the given list. The desired output is [2,4,6,8,10].
  • #1
fred_91
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
 
Technology news on Phys.org
  • #2
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
 
  • #3
Thank you. I hope it looks clearer now.

Any help would be very much appreciated.
 

What is the Process class in Python?

The Process class in Python is a part of the multiprocessing module, which allows for the creation of processes in order to run code concurrently. It essentially provides a framework for managing and controlling multiple processes within a single Python program.

How do you create a process using the Process class?

To create a process using the Process class, you first need to import the multiprocessing module. Then, you can define a function or class that you want to run as a separate process, and use the Process class to create a new process object. Finally, you can start the process by calling the start() method on the process object.

What is the difference between a process and a thread in Python?

A process in Python is a separate instance of a program, with its own memory space and resources, while a thread is a separate flow of execution within a single process. This means that processes can run independently and in parallel, while threads share resources and can only run concurrently.

How do you communicate between processes in Python?

Processes can communicate with each other in Python through a variety of methods, such as pipes, queues, and shared memory. These methods allow for the exchange of data and synchronization between processes, making it easier to coordinate and control their execution.

What are some advantages of using the Process class in Python?

Using the Process class in Python allows for parallel execution of code, which can significantly improve the performance of certain tasks. It also provides a simple and robust way to manage and control multiple processes within a single program, making it easier to develop complex applications.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
7
Views
429
  • Programming and Computer Science
Replies
11
Views
812
  • Programming and Computer Science
Replies
8
Views
794
  • Programming and Computer Science
2
Replies
50
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top