Skip to content

Instantly share code, notes, and snippets.

@Garciat
Created July 24, 2017 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garciat/144a65be9993e0b6c5f57f5009f67ce0 to your computer and use it in GitHub Desktop.
Save Garciat/144a65be9993e0b6c5f57f5009f67ce0 to your computer and use it in GitHub Desktop.
def out_of_process(func):
from types import GeneratorType
GENERATOR = b'#GENERATOR#'
DONE = b'#DONE#'
def runner(conn, *args, **kwargs):
return_value = func(*args, **kwargs)
if isinstance(return_value, GeneratorType):
conn.send(GENERATOR)
for val in return_value:
conn.put(val)
conn.send(DONE)
else:
conn.put(return_value)
conn.close()
def multi_value(proc, return_queue):
while True:
value = return_queue.get()
if isinstance(value, bytes) and value == DONE:
break
yield value
def wrapper(*args, **kwargs):
return_queue = Queue()
args = (return_queue,) + args
proc = Process(target=runner, args=args, kwargs=kwargs)
proc.start()
return_value = return_queue.get()
if isinstance(return_value, bytes) and return_value == GENERATOR:
return multi_value(proc, return_queue)
else:
proc.join()
return return_value
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment