Skip to content

Commit

Permalink
workaround import restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
amunger committed Apr 4, 2024
1 parent 19673bd commit 5b84fba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/kernels/jupyter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
IJupyterConnection,
GetServerOptions,
LiveRemoteKernelConnectionMetadata,
RemoteKernelConnectionMetadata
RemoteKernelConnectionMetadata,
IKernel
} from '../types';
import { ClassType } from '../../platform/ioc/types';
import { ContributedKernelFinderKind, IContributedKernelFinder } from '../internalTypes';
Expand Down Expand Up @@ -257,3 +258,12 @@ export interface IJupyterServerProviderRegistry {
serverProvider: JupyterServerProvider
): JupyterServerCollection;
}

export const IBackgroundThreadService = Symbol('IBackgroundThreadService');
export interface IBackgroundThreadService {
execCodeInBackgroundThread<T>(
kernel: IKernel,
codeWithReturnStatement: string[],
token: CancellationToken
): Promise<T | undefined>;
}
13 changes: 9 additions & 4 deletions src/kernels/variables/pythonVariableRequester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { Telemetry } from '../../telemetry';
import { executeSilently, SilentExecutionErrorOptions } from '../helpers';
import { IKernel } from '../types';
import { IKernelVariableRequester, IJupyterVariable, IVariableDescription } from './types';
import { IDataFrameScriptGenerator, IVariableScriptGenerator, ParentOptions } from '../../platform/common/types';
import { IDataFrameScriptGenerator, IVariableScriptGenerator } from '../../platform/common/types';
import { SessionDisposedError } from '../../platform/errors/sessionDisposedError';
import { execCodeInBackgroundThread } from '../../standalone/api/kernels/backgroundExecution';
import { IBackgroundThreadService } from '../jupyter/types';

type DataFrameSplitFormat = {
index: (number | string)[];
Expand Down Expand Up @@ -76,7 +76,8 @@ async function safeExecuteSilently(
export class PythonVariablesRequester implements IKernelVariableRequester {
constructor(
@inject(IVariableScriptGenerator) private readonly varScriptGenerator: IVariableScriptGenerator,
@inject(IDataFrameScriptGenerator) private readonly dfScriptGenerator: IDataFrameScriptGenerator
@inject(IDataFrameScriptGenerator) private readonly dfScriptGenerator: IDataFrameScriptGenerator,
@inject(IBackgroundThreadService) private readonly backgroundThreadService: IBackgroundThreadService
) {}

public async getDataFrameInfo(
Expand Down Expand Up @@ -187,7 +188,11 @@ export class PythonVariablesRequester implements IKernelVariableRequester {
const options = parent ? { root: parent.root, propertyChain: parent.propertyChain, startIndex } : undefined;
const code = await this.varScriptGenerator.generateCodeToGetAllVariableDescriptions(options);

const content = await execCodeInBackgroundThread<IVariableDescription[]>(kernel, code.split(/\r?\n/), token);
const content = await this.backgroundThreadService.execCodeInBackgroundThread<IVariableDescription[]>(
kernel,
code.split(/\r?\n/),
token
);

if (kernel.disposed || kernel.disposing || token?.isCancellationRequested || !content) {
return [];
Expand Down
13 changes: 13 additions & 0 deletions src/standalone/api/kernels/backgroundExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ import { raceCancellation } from '../../../platform/common/cancellation';
import { getNotebookCellOutputMetadata } from '../../../kernels/execution/helpers';
import { unTrackDisplayDataForExtension } from '../../../kernels/execution/extensionDisplayDataTracker';
import { traceWarning } from '../../../platform/logging';
import { IBackgroundThreadService } from '../../../kernels/jupyter/types';
import { injectable } from 'inversify';

@injectable()
export class BackgroundThreadService implements IBackgroundThreadService {
execCodeInBackgroundThread<T>(
kernel: IKernel,
codeWithReturnStatement: string[],
token: CancellationToken
): Promise<T | undefined> {
return execCodeInBackgroundThread(kernel, codeWithReturnStatement, token);
}
}

export const executionCounters = new WeakMap<IKernel, number>();
export async function execCodeInBackgroundThread<T>(
Expand Down
5 changes: 4 additions & 1 deletion src/standalone/serviceRegistry.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { IExtensionActivationManager, IExtensionSyncActivationService } from '../platform/activation/types';
import { IServiceManager } from '../platform/ioc/types';
import { INotebookExporter, INotebookImporter } from '../kernels/jupyter/types';
import { IBackgroundThreadService, INotebookExporter, INotebookImporter } from '../kernels/jupyter/types';
import { JupyterExporter } from './import-export/jupyterExporter';
import { JupyterImporter } from './import-export/jupyterImporter.node';
import { CommandRegistry as ExportCommandRegistry } from './import-export/commandRegistry';
Expand All @@ -27,6 +27,7 @@ import { EagerlyActivateJupyterUriProviders } from './api/unstable/activateJupyt
import { ExposeUsedAzMLServerHandles } from './api/unstable/usedAzMLServerHandles.deprecated';
import { IExportedKernelServiceFactory } from './api/unstable/types';
import { KernelApi } from './api/kernels/accessManagement';
import { BackgroundThreadService } from './api/kernels/backgroundExecution';

export function registerTypes(context: IExtensionContext, serviceManager: IServiceManager, isDevMode: boolean) {
serviceManager.addSingleton<IExtensionSyncActivationService>(IExtensionSyncActivationService, GlobalActivation);
Expand Down Expand Up @@ -98,4 +99,6 @@ export function registerTypes(context: IExtensionContext, serviceManager: IServi
);

serviceManager.addSingleton<IExtensionSyncActivationService>(IExtensionSyncActivationService, KernelApi);

serviceManager.addSingleton<IBackgroundThreadService>(IBackgroundThreadService, BackgroundThreadService);
}

0 comments on commit 5b84fba

Please sign in to comment.