def _mk_process(self): self.proc = PythonSubProcess(self.exe, self.env) # self.proc.stdin.write('import sys\n') # self.proc.stdin.write('sys.path.append("%s")\n' % PYDEV_SRC) _out, err = self.proc.communicate('from %s import %s\n' % (self.mod, self.func)) if err: raise RuntimeError, "Problem with import: %s" % err
class ExternalFunction(object): '''Emulates a function object with an attached python process ''' def __init__(self, exe, env, module, function, keep): self.exe = exe self.env = env self.mod = module self.func = function self.keep = keep self.thd = None self.proc = None if self.keep: self._mk_process() def _mk_process(self): self.proc = PythonSubProcess(self.exe, self.env) # self.proc.stdin.write('import sys\n') # self.proc.stdin.write('sys.path.append("%s")\n' % PYDEV_SRC) _out, err = self.proc.communicate('from %s import %s\n' % (self.mod, self.func)) if err: raise RuntimeError, "Problem with import: %s" % err def stop(self): '''Stop process ''' if self.proc: self.proc.stop() self.proc = None def __del__(self): self.stop() def __call__(self, *arg, **kwarg): try: if not self.keep or not self.proc: self._mk_process() #parse arguments argsAsString = '' for each in arg: if isinstance(each, str): argsAsString += '"' + str(each) + '", ' else: argsAsString += str(each) + ', ' for key, value in kwarg.items(): if isinstance(value, str): argsAsString += str(key) + '="' + str(value) + '", ' else: argsAsString += str(key) + '=' + str(value) + ', ' argsAsString = argsAsString.strip(',') out, err = self.proc.communicate('print %s(%s)\n' % (self.func, argsAsString)) if err: raise RuntimeError, "Problem with running external process: %s" % err return out finally: if not self.keep: self.stop()
def _mk_process(self): self.proc = PythonSubProcess(self.exe, self.env) # self.proc.stdin.write('import sys\n') # self.proc.stdin.write('sys.path.append("{}")\n'.format(PYDEV_SRC)) _out, err = self.proc.communicate('from scisoftpy import external as _fwext\n') if err: raise RuntimeError('Problem with import: ' + err) _out, err = self.proc.communicate('from {} import {}\n'.format(self.mod, self.func)) if err: raise RuntimeError('Problem with import: ' + err) _out, err = self.proc.communicate('_fwwrapped = _fwext.wrapper({})\n'.format(self.func)) if err: raise RuntimeError('Problem with wrapping: ' + err)
def _mk_process(self): self.proc = PythonSubProcess(self.exe, self.env) # self.proc.stdin.write('import sys\n') # self.proc.stdin.write('sys.path.append("%s")\n' % PYDEV_SRC) _out, err = self.proc.communicate('from scisoftpy import external as _fwext\n') if err: raise RuntimeError, "Problem with import: %s" % err _out, err = self.proc.communicate('from %s import %s\n' % (self.mod, self.func)) if err: raise RuntimeError, "Problem with import: %s" % err _out, err = self.proc.communicate('_fwwrapped = _fwext.wrapper(%s)\n' % self.func) if err: raise RuntimeError, "Problem with wrapping: %s" % err
def _mk_process(self): self.proc = PythonSubProcess(self.exe, self.env) # self.proc.stdin.write('import sys\n') # self.proc.stdin.write('sys.path.append("{}")\n'.format(PYDEV_SRC)) _out, err = self.proc.communicate('from scisoftpy import external as _fwext\n') if err and 'FutureWarning' not in err and not err.startswith('Warning'): raise RuntimeError('Problem with import: ' + err) _out, err = self.proc.communicate('from {} import {}\n'.format(self.mod, self.func)) if err: raise RuntimeError('Problem with import: ' + err) _out, err = self.proc.communicate('_fwwrapped = _fwext.wrapper({})\n'.format(self.func)) if err: raise RuntimeError('Problem with wrapping: ' + err)
class ExternalFunction(object): '''Emulates a function object with an attached python process ''' def __init__(self, exe, env, module, function, keep): self.exe = exe self.env = env self.mod = module # modules = [(k, v.__name__) for k,v in globals().items() if isinstance(v, type(sys)) and not k.startswith('__')] # print func.__name__ # pprint(modules) self.func = function self.keep = keep self.thd = None self.proc = None if self.keep: self._mk_process() def _mk_process(self): self.proc = PythonSubProcess(self.exe, self.env) # self.proc.stdin.write('import sys\n') # self.proc.stdin.write('sys.path.append("{}")\n'.format(PYDEV_SRC)) _out, err = self.proc.communicate('from scisoftpy import external as _fwext\n') if err: raise RuntimeError('Problem with import: ' + err) _out, err = self.proc.communicate('from {} import {}\n'.format(self.mod, self.func)) if err: raise RuntimeError('Problem with import: ' + err) _out, err = self.proc.communicate('_fwwrapped = _fwext.wrapper({})\n'.format(self.func)) if err: raise RuntimeError('Problem with wrapping: ' + err) def stop(self): '''Stop process ''' if self.proc: self.proc.stop() self.proc = None def __del__(self): self.stop() def __call__(self, *arg, **kwarg): import shutil argsdir = save_args((arg, kwarg)) try: if not self.keep or not self.proc: self._mk_process() out, err = self.proc.communicate('_fwiarg, _fwikwarg = _fwext.load_args(\"{}\")\n'.format(argsdir)) # sys.stderr.write('1Out: ' + out + '\n') # sys.stderr.write('1Err: ' + err + '\n') if err: raise RuntimeError('Problem with running external process: ' + err) out, err = self.proc.communicate('print("FWOUT:|{}|".format(_fwext.save_args(_fwwrapped(*_fwiarg, **_fwikwarg))))\n') # sys.stderr.write('2Out: ' + out + '\n') # sys.stderr.write('2Err: ' + err + '\n') if out: for l in out.splitlines(): # sys.stderr.write('3Out: ' + out + '\n') if not l: continue l = l.strip() if l.startswith('FWOUT'): r = l.split('|') if len(r) > 1: d = r[1] try: ret, err = load_args(d) if err: import traceback sys.stderr.write('\n'.join(traceback.format_list(err[2])) + '\n') raise err[1] return ret finally: shutil.rmtree(d) else: print(l) if err: raise RuntimeError('Problem with saving results: ' + err) finally: shutil.rmtree(argsdir) if not self.keep: self.stop()
class ExternalFunction(object): '''Emulates a function object with an attached python process ''' def __init__(self, exe, env, module, function, keep): self.exe = exe self.env = env self.mod = module # modules = [(k, v.__name__) for k,v in globals().items() if isinstance(v, type(sys)) and not k.startswith('__')] # print func.__name__ # pprint(modules) self.func = function self.keep = keep self.thd = None self.proc = None if self.keep: self._mk_process() def _mk_process(self): self.proc = PythonSubProcess(self.exe, self.env) # self.proc.stdin.write('import sys\n') # self.proc.stdin.write('sys.path.append("%s")\n' % PYDEV_SRC) _out, err = self.proc.communicate('from scisoftpy import external as _fwext\n') if err: raise RuntimeError, "Problem with import: %s" % err _out, err = self.proc.communicate('from %s import %s\n' % (self.mod, self.func)) if err: raise RuntimeError, "Problem with import: %s" % err _out, err = self.proc.communicate('_fwwrapped = _fwext.wrapper(%s)\n' % self.func) if err: raise RuntimeError, "Problem with wrapping: %s" % err def stop(self): '''Stop process ''' if self.proc: self.proc.stop() self.proc = None def __del__(self): self.stop() def __call__(self, *arg, **kwarg): import shutil argsdir = save_args((arg, kwarg)) try: if not self.keep or not self.proc: self._mk_process() out, err = self.proc.communicate('_fwiarg, _fwikwarg = _fwext.load_args(\"%s\")\n' % argsdir) # print >> sys.stderr, "1Out:", out # print >> sys.stderr, "1Err:", err if err: raise RuntimeError, "Problem with running external process: %s" % err out, err = self.proc.communicate('print "FWOUT:|%s|" % _fwext.save_args(_fwwrapped(*_fwiarg, **_fwikwarg))\n') # print >> sys.stderr, "2Out:", out # print >> sys.stderr, "2Err:", err if out: for l in out.splitlines(): # print >> sys.stderr, "3Out:", l if not l: continue l = l.strip() if l.startswith('FWOUT'): r = l.split('|') if len(r) > 1: d = r[1] try: ret, err = load_args(d) if err: import traceback print >> sys.stderr, '\n'.join(traceback.format_list(err[2])) raise err[1] return ret finally: shutil.rmtree(d) else: print l if err: raise RuntimeError, "Problem with saving results: %s" % err finally: shutil.rmtree(argsdir) if not self.keep: self.stop()
class ExternalFunction(object): '''Emulates a function object with an attached python process ''' def __init__(self, exe, env, module, function, keep): self.exe = exe self.env = env self.mod = module # modules = [(k, v.__name__) for k,v in globals().items() if isinstance(v, type(sys)) and not k.startswith('__')] # print func.__name__ # pprint(modules) self.func = function self.keep = keep self.thd = None self.proc = None if self.keep: self._mk_process() def _mk_process(self): self.proc = PythonSubProcess(self.exe, self.env) # self.proc.stdin.write('import sys\n') # self.proc.stdin.write('sys.path.append("{}")\n'.format(PYDEV_SRC)) _out, err = self.proc.communicate('from scisoftpy import external as _fwext\n') if err and 'FutureWarning' not in err and not err.startswith('Warning'): raise RuntimeError('Problem with import: ' + err) _out, err = self.proc.communicate('from {} import {}\n'.format(self.mod, self.func)) if err: raise RuntimeError('Problem with import: ' + err) _out, err = self.proc.communicate('_fwwrapped = _fwext.wrapper({})\n'.format(self.func)) if err: raise RuntimeError('Problem with wrapping: ' + err) def stop(self): '''Stop process ''' if self.proc: self.proc.stop() self.proc = None def __del__(self): self.stop() def __call__(self, *arg, **kwarg): import shutil try: argsdir = save_args((arg, kwarg)) except: print("Could not save arguments", file=sys.stderr) raise try: if not self.keep or not self.proc: self._mk_process() out, err = self.proc.communicate('_fwiarg, _fwikwarg = _fwext.load_args(\"{}\")\n'.format(argsdir)) # sys.stderr.write('1Out: ' + out + '\n') # sys.stderr.write('1Err: ' + err + '\n') if err: raise RuntimeError('Problem with running external process: ' + err) out, err = self.proc.communicate('print("FWOUT:|{}|".format(_fwext.save_args(_fwwrapped(*_fwiarg, **_fwikwarg))))\n') # sys.stderr.write('2Out: ' + out + '\n') # sys.stderr.write('2Err: ' + err + '\n') if out: for l in out.splitlines(): # sys.stderr.write('3Out: ' + out + '\n') if not l: continue l = l.strip() if l.startswith('FWOUT'): r = l.split('|') if len(r) > 1: d = r[1] try: ret, err = load_args(d) if err: import traceback sys.stderr.write('\n'.join(traceback.format_list(err[2])) + '\n') raise err[1] return ret finally: shutil.rmtree(d) else: print(l) if err: raise RuntimeError('Problem with saving results: ' + err) finally: shutil.rmtree(argsdir) if not self.keep: self.stop()