Created
December 18, 2018 19:18
-
-
Save MarkFilus/e152a81c3c4319d0d2c0c5be8cced6c1 to your computer and use it in GitHub Desktop.
run as child process function decorator - multiprocessing python
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
from multiprocessing import Pipe, Process | |
def child_process(func): | |
"""Makes the function run as a separate process. Needed for | |
keras classifier with gridsearchcv to work on multiple datasets""" | |
def wrapper(*args, **kwargs): | |
def worker(conn, func, args, kwargs): | |
conn.send(func(*args, **kwargs)) | |
conn.close() | |
parent_conn, child_conn = Pipe() | |
p = Process(target=worker, args=(child_conn, func, args, kwargs)) | |
p.start() | |
ret = parent_conn.recv() | |
p.join() | |
return ret | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment