Skip to content

Instantly share code, notes, and snippets.

@slavanap
Last active January 11, 2021 20:47
Show Gist options
  • Save slavanap/fddb3c77a3cabeffb74277a8efd53b16 to your computer and use it in GitHub Desktop.
Save slavanap/fddb3c77a3cabeffb74277a8efd53b16 to your computer and use it in GitHub Desktop.
Run function in subprocess in Python
#!/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