Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Created August 7, 2023 10:56
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 ehzawad/2d4c220cd79337ed0f675da18c719a89 to your computer and use it in GitHub Desktop.
Save ehzawad/2d4c220cd79337ed0f675da18c719a89 to your computer and use it in GitHub Desktop.
from multiprocessing import Pool, current_process, cpu_count
import os, time
def io_bound_task(num):
start_time = time.time()
print(f"Process ID: {os.getpid()}, Process Name: {current_process().name}, Starting Task {num}")
time.sleep(2) # simulate I/O-bound task with delay
end_time = time.time()
print(f"Process ID: {os.getpid()}, Process Name: {current_process().name}, Finished Task {num}, Execution time: {end_time - start_time} seconds")
if __name__ == "__main__":
st = time.time()
num_processes = cpu_count() # get number of available processors
with Pool(num_processes) as p:
tasks = range(num_processes) # 10 tasks to be done
print(tasks)
p.map(io_bound_task, tasks)
et = time.time()
print(f"Entire execution time: {et - st} seconds")
import os, time
from multiprocessing import cpu_count, current_process
def io_bound_task(num):
start_time = time.time()
print(f"Process ID: {os.getpid()}, Process Name: {current_process().name}, Starting Task {num}")
time.sleep(2) # simulate I/O-bound task with delay
end_time = time.time()
print(f"Process ID: {os.getpid()}, Process Name: {current_process().name}, Finished Task {num}, Execution time: {end_time - start_time} seconds")
if __name__ == "__main__":
st = time.time()
num_processes = cpu_count() # get number of available processors
tasks = range(num_processes) # 10 tasks to be done
print(tasks)
for task in tasks:
io_bound_task(task)
et = time.time()
print(f"Entire execution time: {et - st} seconds")
@ehzawad
Copy link
Author

ehzawad commented Aug 7, 2023

from multiprocessing import Pool, current_process, cpu_count
import os, time

def io_bound_task(num):
start_time = time.time()
print(f"Process ID: {os.getpid()}, Process Name: {current_process().name}, Starting Task {num}")
time.sleep(2) # simulate I/O-bound task with delay
end_time = time.time()
print(f"Process ID: {os.getpid()}, Process Name: {current_process().name}, Finished Task {num}, Execution time: {end_time - start_time} seconds")

def multiProcess():
st = time.time()
num_processes = cpu_count() # get number of available processors
with Pool(num_processes) as p:
tasks = range(num_processes) # 10 tasks to be done
print(tasks)
p.map(io_bound_task, tasks)
et = time.time()
print(f"Entire execution time: {et - st} seconds")

def singleProcess():
st = time.time()
num_processes = cpu_count() # get number of available processors
tasks = range(num_processes) # 10 tasks to be done
print(tasks)
for task in tasks:
io_bound_task(task)
et = time.time()
print(f"Entire execution time: {et - st} seconds")

if name == "main":
singleProcess()
multiProcess()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment