Created
October 13, 2021 21:27
-
-
Save jhuamanchumo/833230e34f9add7713e7469b86820d1f to your computer and use it in GitHub Desktop.
Python multiprocessing example
This file contains hidden or 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
import multiprocessing as mp | |
import time | |
result_list_pool = [] | |
result_list_process = [] | |
def foo_pool(x): | |
time.sleep(2) | |
return x*x | |
def foo_pool_p(x, conn): | |
time.sleep(2) | |
conn.send(x*x) | |
conn.close | |
def log_result(result): | |
# This is called whenever foo_pool(i) returns a result. | |
# result_list is modified only by the main process, not the pool workers. | |
result_list_pool.append(result) | |
def apply_async_with_callback(): | |
pool = mp.Pool() | |
print(" pool with with {} processes".format(pool._processes)) | |
for i in range(8): | |
pool.apply_async(foo_pool, args=(i, ), callback=log_result) | |
pool.close() | |
pool.join() | |
print(result_list_pool) | |
def paralell_process(): | |
# create a list to keep all processes | |
processes = [] | |
# create a list to keep connections | |
parent_connections = [] | |
# create 8 processes | |
for i in range(8): | |
# create a pipe for communication | |
parent_conn, child_conn = mp.Pipe() | |
parent_connections.append(parent_conn) | |
# create the process, pass arg and connection | |
process = mp.Process(target=foo_pool_p, args=(i, child_conn,)) | |
processes.append(process) | |
# start all processes | |
for process in processes: | |
process.start() | |
# make sure that all processes have finished | |
for process in processes: | |
process.join() | |
# collect result from processes | |
count = 0 | |
for parent_connection in parent_connections: | |
value = parent_connection.recv() | |
result_list_process.append(value) | |
print(result_list_process) | |
if __name__ == '__main__': | |
print("running with pool.apply_async") | |
start = time.perf_counter() | |
apply_async_with_callback() | |
end = time.perf_counter() | |
print("time: ", round(end-start, 2)) | |
print("-----------------------------------------------------") | |
print("running with Process") | |
start = time.perf_counter() | |
paralell_process() | |
end = time.perf_counter() | |
print("time: ", round(end-start, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment