Skip to content

Instantly share code, notes, and snippets.

@denisrasulev
Created April 15, 2019 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denisrasulev/34f8069f7b7dd54f5b678e0487112b0e to your computer and use it in GitHub Desktop.
Save denisrasulev/34f8069f7b7dd54f5b678e0487112b0e to your computer and use it in GitHub Desktop.
Multiprocessing using Python 3
#!/usr/bin/env python3
# Importing all needed modules
import multiprocessing
import random
import time
import timeit
print("Starting Multiprocessing...")
# Starting timer which keep track of the runtime
start_time = timeit.default_timer()
# Define the function which will be executed within the pool
def asyncProcess(processRuntime, processID):
"""
Sleeps the amount of time as seconds from variable processRuntime
returns: <STRING>
"""
time.sleep(processRuntime)
return "%i: This Process ran %i seconds" % (processID, processRuntime)
if __name__ == "__main__":
# Define Pool Size of maximal concurrent processes
pool_size = 10
# Define an empty list where we store child processes
processes = []
# Define an empty list to store finished processes
finished_processes = []
# Define an empty pool whith maximal concurrent processes
pool = multiprocessing.Pool(processes=pool_size)
# Firing in total of 10 processes
for i in range(0, pool_size):
# Append the process variable with a process object
processes.append(
pool.apply_async(asyncProcess, args=(random.randint(0, 10), i))
)
# Closes the pool to stop accepting new processes
pool.close()
# Synchronous Pool
pool.join()
# Iterate through the processes variable
for process in processes:
# Print the process returned value
print(process.get())
print(
"Parent: this Process ran %s seconds" % str(timeit.default_timer() - start_time)
)
# Asynchronous Pool
# Iterate through processes as long as a process is running
while True:
# Iterate through the processes variable
for process in processes:
# Check if process is done and not in finished_processes
if(process.ready() and process not in finished_processes):
# Print the returned value
print(process.get())
# Append the finished process to finished_processes
finished_processes.append(process)
# Break while loop when finished_processes length equal processes length
if len(finished_processes) == len(processes):
break
print("Parent: this Process ran %s seconds" % str(timeit.default_timer() - start_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment