microsoft / jupyter-core

Library for writing Jupyter kernels in .NET Core
MIT License
108 stars 23 forks source link

Add CancellationToken for cell execution #61

Closed rmshaffer closed 4 years ago

rmshaffer commented 4 years ago

Following up on @cgranade's suggestion in #59, this PR adds a CancellationToken parameter to the various cell execution methods. By doing it in this way, the interrupt message is abstracted away, and consuming kernels need only to worry about the CancellationToken.

This is a non-breaking change. Existing consumers are unaffected. If consumers wish to support cancellation, there are two things they can optionally do:

  1. To support cancellation for magic symbol execution: Derive custom magic symbols from CancellableMagicSymbol rather than MagicSymbol, and assign the appropriate function to the ExecuteCancellable property.
  2. To support cancellation for mundane cell execution: Override the implementation of BaseEngine.ExecuteMundane(string, IChannel, CancellationToken).

I've bumped the minor version number because this is a relatively significant piece of functionality that consumers may want to update their code to take advantage of.

rmshaffer commented 4 years ago

I found a simple way to do this in a non-breaking way. Working on that now, will re-publish this with those changes. (I'll still bump the minor version, though, since I think it's a big enough feature that it's worth it.)

rmshaffer commented 4 years ago

I found a simple way to do this in a non-breaking way. Working on that now, will re-publish this with those changes. (I'll still bump the minor version, though, since I think it's a big enough feature that it's worth it.)

The changes should be ready now. I've updated the PR description with details.

cgranade commented 4 years ago

Awesome, @rmshaffer! I think that does a great job of adding cancellation functionality without breaking anything. That seems especially nice for lighter weight magic commands like lsmagic and config that probably don't need cancellation support, while still letting %simulate and %azure.* use the new interrupt functionality.

rmshaffer commented 4 years ago

Thanks @cgranade for the suggestion and for reviewing the changes! I’ll merge this, do some more verification of existing consumers, and then push a new release if everything looks good.