Add decorator for handing child-process side of pipe
This commit is contained in:
@@ -2,6 +2,8 @@ import logging
|
||||
from multiprocessing import Process, Pipe
|
||||
from bpy.types import Operator
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
def subprocess_operator(cls: Operator, polling_interval=.01) -> Operator:
|
||||
"""
|
||||
Class decorator which wraps Operator methods with setup code for running a subprocess.
|
||||
@@ -34,9 +36,10 @@ def subprocess_operator(cls: Operator, polling_interval=.01) -> Operator:
|
||||
|
||||
def decorate_invoke(orig_invoke):
|
||||
def invoke(self, context, event):
|
||||
self.pipe = Pipe()
|
||||
|
||||
orig_invoke(self, context, event)
|
||||
|
||||
self.pipe = Pipe()
|
||||
self.proc = Process(
|
||||
*getattr(self, 'proc_args', []),
|
||||
**getattr(self, 'proc_kwargs', [])
|
||||
@@ -57,15 +60,19 @@ def subprocess_operator(cls: Operator, polling_interval=.01) -> Operator:
|
||||
self.log.debug("polling")
|
||||
try:
|
||||
if self.pipe[0].poll():
|
||||
newdata = self.pipe[0].recv()
|
||||
resp = self.pipe[0].recv()
|
||||
assert(isinstance(resp, SubprocessMessage))
|
||||
else:
|
||||
newdata = None
|
||||
resp = None
|
||||
except EOFError:
|
||||
self.log.debug("done polling")
|
||||
return {'FINISHED'}
|
||||
|
||||
if newdata is not None:
|
||||
self.handle_response(newdata) #TODO: make this a customizable callback
|
||||
if resp is not None:
|
||||
if resp.exception is not None:
|
||||
raise resp.exception
|
||||
elif resp.data is not None:
|
||||
self.handle_response(resp.data) #TODO: make this a customizable callback
|
||||
# this should allow chaining of multiple subprocess in a single operator
|
||||
|
||||
return {'PASS_THROUGH'}
|
||||
@@ -75,3 +82,28 @@ def subprocess_operator(cls: Operator, polling_interval=.01) -> Operator:
|
||||
setattr(cls, 'poll_subprocess', poll_subprocess)
|
||||
|
||||
return cls
|
||||
|
||||
def subprocess_function(func, *args, pipe, **kwargs):
|
||||
"""
|
||||
Wrapper which feeds the return val of `func` into the latter end of a pipe
|
||||
"""
|
||||
def wrapper(*args, **kwargs):
|
||||
# we have to explicitly close the end of the pipe we are NOT using,
|
||||
# otherwise no exception will be generated when the other process closes its end.
|
||||
pipe[0].close()
|
||||
|
||||
try:
|
||||
return_val = func(*args, **kwargs)
|
||||
pipe[1].send(SubprocessMessage(data=return_val))
|
||||
except Exception as err:
|
||||
log.debug("Caught exception from subprocess: %s", err)
|
||||
pipe[1].send(SubprocessMessage(exception=err))
|
||||
finally:
|
||||
pipe[1].close()
|
||||
|
||||
return wrapper
|
||||
|
||||
class SubprocessMessage:
|
||||
def __init__(self, exception=None, data=None):
|
||||
self.exception = exception
|
||||
self.data = data
|
||||
|
Reference in New Issue
Block a user