Trying to play around with the multiprocessing module, and I used the join method, but it seems that it does not prevent the interpreter to wait for the processes to finish before it moves out to te rest of the script, here is my code (and you will understand better when you read the outpu):
import multiprocessing
import time
start = time.perf_counter()
# creating simple sleep function
def sleeping(s):
print(f'Sleeping for {s} second(s)...')
time.sleep(s)
print('Just woke up!')
# creating processes list
processes = []
# creating processes
if __name__ == '__main__':
for _ in range(10):
p = multiprocessing.Process(target=sleeping, args=(1,))
p.start()
processes.append(p)
for p in processes:
p.join()
finish = time.perf_counter()
print(f'*****************Finished processing in {round(finish-start, 2)} second(s)')
Here is the output that I get:
*****************Finished processing in 0.0 second(s)
Sleeping for 1 second(s)...
*****************Finished processing in 0.0 second(s)
Sleeping for 1 second(s)...
*****************Finished processing in 0.0 second(s)
Sleeping for 1 second(s)...
*****************Finished processing in 0.0 second(s)
Sleeping for 1 second(s)...
*****************Finished processing in 0.0 second(s)
Sleeping for 1 second(s)...
*****************Finished processing in 0.0 second(s)
Sleeping for 1 second(s)...
*****************Finished processing in 0.0 second(s)
Sleeping for 1 second(s)...
*****************Finished processing in 0.0 second(s)
Sleeping for 1 second(s)...
*****************Finished processing in 0.0 second(s)
Sleeping for 1 second(s)...
*****************Finished processing in 0.0 second(s)
Sleeping for 1 second(s)...
Just woke up!
Just woke up!
Just woke up!Just woke up!
Just woke up!
Just woke up!
Just woke up!
Just woke up!Just woke up!
Just woke up!
*****************Finished processing in 1.12 second(s)
Also, I would like to understand why in some lines of the output it does not return to the next line and instead just prints the text next to the previous one.