Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract send_execute_reply method from Kernel.execute_request #7713

Closed
wants to merge 1 commit into from

Conversation

kmike
Copy link
Contributor

@kmike kmike commented Feb 9, 2015

This is related to #7614: I want do_executemethod to be async. Currently the whole execute_request should be copy-pasted to implement it; after this refactoring it is possible to return a deferred from do_execute and add a callback to it in overridden send_execute_reply which calls parent's send_execute_reply.

Example:

class MyKernel(Kernel):

    # ...

    def send_execute_reply(self, stream, ident, parent, md, reply_content):
        def done(result):
            return super(MyKernel, self).send_execute_reply(stream, ident, parent, md, result)
        assert isinstance(reply_content, defer.Deferred)
        reply_content.addCallback(done)

    def do_execute(self, code, silent, store_history=True, user_expressions=None,
                   allow_stdin=False):

        def publish_result(text):
            if not silent:
                stream_content = {'name': 'stdout', 'text': text}
                self.send_response(self.iopub_socket, 'stream', stream_content)

        def success(result):
            text = self.to_text(result)
            publish_result(text)
            return {
                'status': 'ok',
                'execution_count': self.execution_count,
                'payload': [],
                'user_expressions': {},
            }

        def error(failure):
            text = str(failure)
            publish_result(text)
            return {
                'status': 'error',
                'execution_count': self.execution_count,
                'ename': '',
                'evalue': text,
                'traceback': []
            }

        d = self.run_async(code)
        d.addCallbacks(success, error)
        return d

@@ -699,3 +701,14 @@ def _at_shutdown(self):
self.session.send(self.iopub_socket, self._shutdown_message, ident=self._topic('shutdown'))
self.log.debug("%s", self._shutdown_message)
[ s.flush(zmq.POLLOUT) for s in self.shell_streams ]

def _flush_execute_output(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming point: 'execute output' implies something a bit different. I would call this _flush_stdstreams.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is specific to execute command because it sleeps for _execute_sleep delay, that's why I added 'execute' to the name. But there are other places where this function can be useful - should they also sleep? If so, I think it could make sense to rename _execute_sleep.

@minrk
Copy link
Member

minrk commented Feb 9, 2015

Does this logic suggest that all replies should be separated from their handler methods, rather than just execute replies?

# Handle the reply message
content = parent[u'content']
stop_on_error = content.get('stop_on_error', True)
silent = content[u'silent']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stop_on_error and silent are only used once below, it's probably not necessary to assign them to variables again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd even add another variable for reply_msg['content']['status'] == u'error' to make the condition below nicer :) I'll remove it if you want.

@takluyver
Copy link
Member

In principle I think this is fine.

@kmike
Copy link
Contributor Author

kmike commented Feb 9, 2015

@minrk a good point. Generally, yes; it is just that copy-paste savings are smaller for other methods.

@minrk minrk added this to the 4.0 milestone Feb 9, 2015
@ellisonbg
Copy link
Member

Do you plan on working on this soon? If not, do you mind if we close it until work restarts? We are doing major refactoring in the repo (our "Big Split") and existing PRs are going to bit-rot quickly (already).

@kmike
Copy link
Contributor Author

kmike commented Mar 29, 2015

Hi @ellisonbg,

No, I don't have plans to work on it soon. I'll open a new PR if I have time in future.

For the record: the current hack-ish solution lives here; there is one more problem with async methods which is worked around there ("idle" event was published too early).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants