this is my first time posting on stackoverflow, so I hope I do not mess it up.
I am a beginner in Python, and I wanted to attempt Multi-processing. But I've spent two days following basic tutorials about it, and none of them seem to work the way I want it to. An example of what I'm trying:
import os
from multiprocessing import Process, current_process
def square(number):
result = number * number
proc_id = os.getpid()
print(f"Process ID: {proc_id}")
process_name = current_process().name
print(f"Process Name: {process_name}")
print(f"The number {number} squares to {result}.")
if __name__ == "__main__":
processes = []
numbers = [1, 2, 3, 4, 5]
for i, number in enumerate(numbers):
process = Process(target=square, args=(number,))
processes.append(process)
process.start()
for process in processes:
process.join()
A simple code, but all that comes out on the compiler is blank.
Yet if I change this line here:
process = Process(target=square, args=(number,))
To:
process = Process(target=square(number))
It works now! But the output would then be wrong.
My ideal output
Process ID: 10000
Process Name: Process - 1
The number 1 squares to 1.
Process ID: 10001
Process Name: Process - 2
The number 2 squares to 4.
Process ID: 10002
Process Name: Process - 3
The number 3 squares to 9.
Process ID: 10003
Process Name: Process - 4
The number 4 squares to 16.
Actual output
Process ID: 10000
Process Name: MainProcess
The number 1 squares to 1.
Process ID: 10000
Process Name: MainProcess
The number 2 squares to 4.
Process ID: 10000
Process Name: MainProcess
The number 3 squares to 9.
Process ID: 10000
Process Name: MainProcess
The number 4 squares to 16.
I'm using Windows 10, Visual Studio 2019, Python 3.7.
I'm not sure if the solution is simple, but my brain is square and I need help.