-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hold a separate kernel instance for each notebook to avoid interference
- Loading branch information
1 parent
ee026af
commit cf84a47
Showing
9 changed files
with
71 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.Azure.Functions.Extensions.DependencyInjection; | ||
using Microsoft.DotNet.Interactive.CSharp; | ||
using Microsoft.DotNet.Interactive; | ||
using System.Collections.Concurrent; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using System; | ||
|
||
[assembly: FunctionsStartup(typeof(blazoract.Api.Startup))] | ||
|
||
namespace blazoract.Api | ||
{ | ||
// To avoid interference across notebooks, we need a separate kernel instance per notebook. | ||
// Set up a cache in memory that holds a certain maximum number of instances, and evicts | ||
// entries if they become idle for longer than a certain period. | ||
|
||
public class KernelStore | ||
{ | ||
const int MaxEntries = 1024; | ||
const int MaxIdleMinutes = 15; | ||
|
||
private MemoryCache _kernelsCache = new MemoryCache(new MemoryCacheOptions | ||
{ | ||
SizeLimit = MaxEntries | ||
}); | ||
|
||
private ConcurrentDictionary<string, CompositeKernel> _kernels | ||
= new ConcurrentDictionary<string, CompositeKernel>(); | ||
|
||
public CompositeKernel GetKernelForNotebook(string notebookId) | ||
{ | ||
return _kernelsCache.GetOrCreate(notebookId, entry => | ||
{ | ||
entry.SetSlidingExpiration(TimeSpan.FromMinutes(MaxIdleMinutes)); | ||
entry.Size = 1; | ||
return new CompositeKernel() | ||
{ | ||
new CSharpKernel().UseDefaultFormatting().UseDotNetVariableSharing() | ||
}; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters