def execute(inputfile, outputfile): """ Executor. Args: name of the inputfile, which is a pickled object that contains the function to be executed, it's args, kwargs etc. name of the outputfile, where the outputs from the computation are to be pickled are written """ all_names = dir(__builtins__) user_ns = locals() user_ns.update( {'__builtins__': {k: getattr(__builtins__, k) for k in all_names}}) bufs = None with open(inputfile, 'rb') as pickled_bufs: bufs = pickle.load(pickled_bufs) f, args, kwargs = unpack_apply_message(bufs, user_ns, copy=False) fname = getattr(f, '__name__', 'f') prefix = "kotta_" fname = prefix + "f" argname = prefix + "args" kwargname = prefix + "kwargs" resultname = prefix + "result" user_ns.update({ fname: f, argname: args, kwargname: kwargs, resultname: resultname }) code = "{0} = {1}(*{2}, **{3})".format(resultname, fname, argname, kwargname) try: print("[Kotta_DBG] Executing : {0}".format(code)) print("[Kotta_DBG] Func : {0}".format(f)) exec(code, user_ns, user_ns) finally: #print("Done : {0}".format(locals())) print("[Kotta_DBG] Result : {0}".format(user_ns.get(resultname))) with open(outputfile, 'wb') as outfile: pickle.dump(user_ns.get(resultname), outfile) return True
def execute(inputfile, outputfile): """ Executor. Args: name of the inputfile, which is a pickled object that contains the function to be executed, it's args, kwargs etc. name of the outputfile, where the outputs from the computation are to be pickled are written """ all_names = dir(__builtins__) user_ns = locals() user_ns.update( {'__builtins__' : {k : getattr(__builtins__, k) for k in all_names} } ) bufs = None with open(inputfile, 'rb') as pickled_bufs: bufs = pickle.load(pickled_bufs) f, args, kwargs = unpack_apply_message(bufs, user_ns, copy=False) fname = getattr(f, '__name__', 'f') prefix = "kotta_" fname = prefix+"f" argname = prefix+"args" kwargname = prefix+"kwargs" resultname = prefix+"result" user_ns.update({ fname : f, argname : args, kwargname : kwargs, resultname : resultname }) code = "{0} = {1}(*{2}, **{3})".format(resultname, fname, argname, kwargname) try: print("[Kotta_DBG] Executing : {0}".format(code)) print("[Kotta_DBG] Func : {0}".format(f)) exec(code, user_ns, user_ns) finally : #print("Done : {0}".format(locals())) print("[Kotta_DBG] Result : {0}".format(user_ns.get(resultname))) with open(outputfile, 'wb') as outfile: pickle.dump(user_ns.get(resultname), outfile) return True
def apply_request(self, stream, ident, parent): try: content = parent[u'content'] bufs = parent[u'buffers'] msg_id = parent['header']['msg_id'] except: self.log.error("Got bad msg: %s", parent, exc_info=True) return # Set the parent message of the display hook and out streams. self.shell.displayhook.set_parent(parent) self.shell.display_pub.set_parent(parent) sys.stdout.set_parent(parent) sys.stderr.set_parent(parent) # pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent) # self.iopub_socket.send(pyin_msg) # self.session.send(self.iopub_socket, u'pyin', {u'code':code},parent=parent) sub = self._make_subheader() try: working = self.shell.user_ns prefix = "_"+str(msg_id).replace("-","")+"_" f,args,kwargs = unpack_apply_message(bufs, working, copy=False) fname = getattr(f, '__name__', 'f') fname = prefix+"f" argname = prefix+"args" kwargname = prefix+"kwargs" resultname = prefix+"result" ns = { fname : f, argname : args, kwargname : kwargs , resultname : None } # print ns working.update(ns) code = "%s = %s(*%s,**%s)" % (resultname, fname, argname, kwargname) try: exec code in self.shell.user_global_ns, self.shell.user_ns result = working.get(resultname) finally: for key in ns.iterkeys(): working.pop(key) packed_result,buf = serialize_object(result) result_buf = [packed_result]+buf except: exc_content = self._wrap_exception('apply') # exc_msg = self.session.msg(u'pyerr', exc_content, parent) self.session.send(self.iopub_socket, u'pyerr', exc_content, parent=parent, ident=self._topic('pyerr')) reply_content = exc_content result_buf = [] if exc_content['ename'] == 'UnmetDependency': sub['dependencies_met'] = False else: reply_content = {'status' : 'ok'} # put 'ok'/'error' status in header, for scheduler introspection: sub['status'] = reply_content['status'] # flush i/o sys.stdout.flush() sys.stderr.flush() reply_msg = self.session.send(stream, u'apply_reply', reply_content, parent=parent, ident=ident,buffers=result_buf, subheader=sub)
def apply_request(self, stream, ident, parent): try: content = parent[u'content'] bufs = parent[u'buffers'] msg_id = parent['header']['msg_id'] except: self.log.error("Got bad msg: %s", parent, exc_info=True) return self._publish_status(u'busy', parent) # Set the parent message of the display hook and out streams. shell = self.shell shell.displayhook.set_parent(parent) shell.display_pub.set_parent(parent) sys.stdout.set_parent(parent) sys.stderr.set_parent(parent) # pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent) # self.iopub_socket.send(pyin_msg) # self.session.send(self.iopub_socket, u'pyin', {u'code':code},parent=parent) sub = self._make_subheader() try: working = shell.user_ns prefix = "_"+str(msg_id).replace("-","")+"_" f,args,kwargs = unpack_apply_message(bufs, working, copy=False) fname = getattr(f, '__name__', 'f') fname = prefix+"f" argname = prefix+"args" kwargname = prefix+"kwargs" resultname = prefix+"result" ns = { fname : f, argname : args, kwargname : kwargs , resultname : None } # print ns working.update(ns) code = "%s = %s(*%s,**%s)" % (resultname, fname, argname, kwargname) try: exec code in shell.user_global_ns, shell.user_ns result = working.get(resultname) finally: for key in ns.iterkeys(): working.pop(key) packed_result,buf = serialize_object(result) result_buf = [packed_result]+buf except: # invoke IPython traceback formatting shell.showtraceback() # FIXME - fish exception info out of shell, possibly left there by # run_code. We'll need to clean up this logic later. reply_content = {} if shell._reply_content is not None: reply_content.update(shell._reply_content) e_info = dict(engine_uuid=self.ident, engine_id=self.int_id, method='apply') reply_content['engine_info'] = e_info # reset after use shell._reply_content = None self.session.send(self.iopub_socket, u'pyerr', reply_content, parent=parent, ident=self._topic('pyerr')) result_buf = [] if reply_content['ename'] == 'UnmetDependency': sub['dependencies_met'] = False else: reply_content = {'status' : 'ok'} # put 'ok'/'error' status in header, for scheduler introspection: sub['status'] = reply_content['status'] # flush i/o sys.stdout.flush() sys.stderr.flush() reply_msg = self.session.send(stream, u'apply_reply', reply_content, parent=parent, ident=ident,buffers=result_buf, subheader=sub) self._publish_status(u'idle', parent)