class_docstr: Extend IPython kernel for parallel computing
class_docstr_tok: ["Extend","IPython","kernel","for","parallel","computing"]
code: def do_apply(self, content, bufs, msg_id, reply_metadata):
      shell = self.shell
      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 = f"{resultname} = {fname}(*{argname},**{kwargname})"
          try:
              exec(code, shell.user_global_ns, shell.user_ns)
              result = working.get(resultname)
          finally:
              for key in ns:
                  working.pop(key)
  
          result_buf = serialize_object(
              result,
              buffer_threshold=self.session.buffer_threshold,
              item_threshold=self.session.item_threshold,
          )
  
      except BaseException as e:
          # invoke IPython traceback formatting
          # this sends the 'error' message
          try:
              shell.showtraceback()
          except Exception as tb_error:
              self.log.error(
                  f"Failed to show traceback for {e}: {tb_error}"
              )
  
          try:
              str_evalue = str(e)
          except Exception as str_error:
              str_evalue = (
                  f"Failed to cast exception to string: {str_error}"
              )
          reply_content = {
              "traceback": [],
              "ename": str(type(e).__name__),
              "evalue": str_evalue,
          }
          # get formatted traceback, which ipykernel recorded
          if hasattr(shell, "_last_traceback"):
              # ipykernel 4.4
              reply_content["traceback"] = shell._last_traceback or []
          else:
              self.log.warning(
                  "Didn't find a traceback where I expected to"
              )
          shell._last_traceback = None
          reply_content["engine_info"] = self.get_engine_info(
              method="apply"
          )
  
          self.log.info(
              "Exception in apply request:\n%s",
              "\n".join(reply_content["traceback"]),
          )
          result_buf = []
          reply_content["status"] = "error"
      else:
          reply_content = {"status": "ok"}
  
      return reply_content, result_buf
  
imports: import asyncio
  import inspect
  import sys
  from functools import partial
  import ipykernel
  from ipykernel.ipkernel import IPythonKernel
  from traitlets import Integer, Type
  from ipyparallel.serialize import serialize_object, unpack_apply_message
  from ipyparallel.util import utcnow
  from .datapub import ZMQDataPublisher
label_desc: Use image node_id 10 for calling the IPythonParallelKernel obj's underlying member method code with example usage: obj.do_apply(content, bufs, msg_id, reply_metadata) and returns: reply_content,  result_buf