Пример #1
0
    def async_run_code(self, code_obj, result=None, async_=False):
        """A monkey-patched replacement for the InteractiveShell.run_code method.
        It runs the in a separate thread and calls QApplication.processEvents
        periodically until the method finishes.

        :param code_obj : code object
          A compiled code object, to be executed
        :param result : ExecutionResult, optional
          An object to store exceptions that occur during execution.
        :param async_ : Bool (Experimental))
           Attempt to run top-level asynchronous code in a default loop.

        :returns: An AsyncTaskResult object
        """
        # Different target arguments depending on IPython's version
        function_parameters = getfullargspec(orig_run_code)
        if 'result' in function_parameters.args:
            if hasattr(function_parameters, 'kwonlyargs'
                       ) and 'async_' in function_parameters.kwonlyargs:
                return orig_run_code(
                    code_obj, result,
                    async_=async_)  # return coroutine to be awaited
            else:
                task = BlockingAsyncTaskWithCallback(
                    target=orig_run_code,
                    args=(code_obj, result),
                    blocking_cb=QApplication.processEvents)
        else:
            task = BlockingAsyncTaskWithCallback(
                target=orig_run_code,
                args=(code_obj, ),
                blocking_cb=QApplication.processEvents)
        return task.start()
def our_run_code(self, code_obj, result=None, async_=False):
    """ Method with which we replace the run_code method of IPython's InteractiveShell class.
        It calls the original method (renamed to ipython_run_code) on a separate thread
        so that we can avoid locking up the whole of MantidPlot while a command runs.

        Parameters
        ----------
        code_obj : code object
          A compiled code object, to be executed
        result : ExecutionResult, optional
          An object to store exceptions that occur during execution.
        async_ : Bool (Experimental))
           Attempt to run top-level asynchronous code in a default loop.
        Returns
        -------
        False : Always, as it doesn't seem to matter.
    """
    # Different target arguments depending on IPython's version
    function_parameters = getfullargspec(self.ipython_run_code)
    if 'result' in function_parameters.args:
        if hasattr(function_parameters, 'kwonlyargs') and 'async_' in function_parameters.kwonlyargs:
            return self.ipython_run_code(code_obj, result, async_=async_)  # return coroutine to be awaited
        else:
            t = threading.Thread(target=self.ipython_run_code, args=(code_obj, result))
    else:
        t = threading.Thread(target=self.ipython_run_code, args=(code_obj,))
    t.start()
    while t.is_alive():
        QtGui.QApplication.processEvents()
    # We don't capture the return value of the ipython_run_code method but as far as I can tell
    #   it doesn't make any difference what's returned
    return 0
Пример #3
0
 def async_run_code(self, code_obj, result=None):
     """A monkey-patched replacement for the InteractiveShell.run_code method.
     It runs the in a separate thread and calls QApplication.processEvents
     periodically until the method finishes
     """
     # ipython 3.0 introduces a third argument named result
     if len(getfullargspec(orig_run_code).args) == 3:
         args = (code_obj, result)
     else:
         args = (code_obj, )
     task = BlockingAsyncTaskWithCallback(
         target=orig_run_code,
         args=args,
         blocking_cb=QApplication.processEvents)
     return task.start()