Created
August 7, 2023 10:56
-
-
Save ehzawad/2d4c220cd79337ed0f675da18c719a89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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()