I have a assignment for my course in Python.
The question is this :
You will write a program creating multiple processes (p=10). Each process will receive integer value (y) and compute y multiplying by 4 (y*4). A collection of y values is a list of integers [0, 1, 2, 3, 4, 5]. You must use a process pool, so you need to map these 10 processes to take the list of y integers. Do the following:
Your program should print out the following three. (1) Print out the original list. (2) Print each running process name and its output of y*4 in random order of execution
Output of (1): Input list: [0, 1, 2, 3, 4, 5]
Output of (2): Output in random order:
PoolWorker-10 output: [0, 4, 8, 12, 16, 20]
PoolWorker-11 output: [0, 4, 8, 12, 16, 20]
PoolWorker-12 output: [0, 4, 8, 12, 16, 20]
My first attempt was:
import time
from multiprocessing import Pool
from multiprocessing import Process, current_process
def f(number):
result=4*number
Process_name=current_process().name
print(f"Poolworker -{Process_name} output: {result} ")
return result
if __name__=='__main__':
p= Pool(processes=10)
numbers=[0,1,2,3,4,5]
result=p.map(f,numbers)
The output was:
Poolworker -SpawnPoolWorker-2 output: 0
Poolworker -SpawnPoolWorker-2 output: 4
Poolworker -SpawnPoolWorker-2 output: 8
Poolworker -SpawnPoolWorker-2 output: 12
Poolworker -SpawnPoolWorker-2 output: 16
Poolworker -SpawnPoolWorker-2 output: 20
My second attempt:
import time
from multiprocessing import Pool
from multiprocessing import Process, current_process
def f(*number):
numbers=list(number)
i=0
for x in numbers:
numbers[i]=4*numbers[i]
i+=1
Process_name=current_process().name
print(f"Poolworker -{Process_name}output: {numbers} ")
return numbers
if __name__ == '__main__':
array=[0,1,2,3,4,5]
p=Pool(processes=10)
result=p.map(f,array)
I still have the wrong output with the changes.
How can I get the desired output?