Last active
January 11, 2021 20:47
-
-
Save slavanap/fddb3c77a3cabeffb74277a8efd53b16 to your computer and use it in GitHub Desktop.
Run function in subprocess in Python
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# slavanap@gitlab | |
def subprocess(func): | |
def wrapper(v, kv, w): | |
w.send(func(*v, **kv)) | |
w.close() | |
import multiprocessing | |
def realfunc(*vargs, **kvargs): | |
r, w = multiprocessing.Pipe() | |
p = multiprocessing.Process(target=wrapper, args=(vargs, kvargs, w)) | |
p.start() | |
p.join() | |
if p.exitcode != 0: | |
raise OSError("Subprocess exited with an error") | |
return r.recv() | |
return realfunc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment