forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use worker threads for fetching conda environments and interpreter re…
…lated info (microsoft#22481)
- Loading branch information
Kartik Raj
authored
Nov 19, 2023
1 parent
3c552f9
commit f6e1338
Showing
43 changed files
with
537 additions
and
91 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { Worker } from 'worker_threads'; | ||
import * as path from 'path'; | ||
import { traceVerbose, traceError } from '../../../logging/index'; | ||
|
||
/** | ||
* Executes a worker file. Make sure to declare the worker file as a entry in the webpack config. | ||
* @param workerFileName Filename of the worker file to execute, it has to end with ".worker.js" for webpack to bundle it. | ||
* @param workerData Arguments to the worker file. | ||
* @returns | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
export async function executeWorkerFile(workerFileName: string, workerData: any): Promise<any> { | ||
if (!workerFileName.endsWith('.worker.js')) { | ||
throw new Error('Worker file must end with ".worker.js" for webpack to bundle webworkers'); | ||
} | ||
return new Promise((resolve, reject) => { | ||
const worker = new Worker(workerFileName, { workerData }); | ||
const id = worker.threadId; | ||
traceVerbose( | ||
`Worker id ${id} for file ${path.basename(workerFileName)} with data ${JSON.stringify(workerData)}`, | ||
); | ||
worker.on('message', (msg: { err: Error; res: unknown }) => { | ||
if (msg.err) { | ||
reject(msg.err); | ||
} | ||
resolve(msg.res); | ||
}); | ||
worker.on('error', (ex: Error) => { | ||
traceError(`Error in worker ${workerFileName}`, ex); | ||
reject(ex); | ||
}); | ||
worker.on('exit', (code) => { | ||
traceVerbose(`Worker id ${id} exited with code ${code}`); | ||
if (code !== 0) { | ||
reject(new Error(`Worker ${workerFileName} stopped with exit code ${code}`)); | ||
} | ||
}); | ||
}); | ||
} |
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,16 @@ | ||
import { parentPort, workerData } from 'worker_threads'; | ||
import { _workerPlainExecImpl } from './workerRawProcessApis'; | ||
|
||
_workerPlainExecImpl(workerData.file, workerData.args, workerData.options) | ||
.then((res) => { | ||
if (!parentPort) { | ||
throw new Error('Not in a worker thread'); | ||
} | ||
parentPort.postMessage({ res }); | ||
}) | ||
.catch((err) => { | ||
if (!parentPort) { | ||
throw new Error('Not in a worker thread'); | ||
} | ||
parentPort.postMessage({ err }); | ||
}); |
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,26 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { SpawnOptions } from 'child_process'; | ||
import * as path from 'path'; | ||
import { executeWorkerFile } from './main'; | ||
import { ExecutionResult, ShellOptions } from './types'; | ||
|
||
export function workerShellExec(command: string, options: ShellOptions): Promise<ExecutionResult<string>> { | ||
return executeWorkerFile(path.join(__dirname, 'shellExec.worker.js'), { | ||
command, | ||
options, | ||
}); | ||
} | ||
|
||
export function workerPlainExec( | ||
file: string, | ||
args: string[], | ||
options: SpawnOptions = {}, | ||
): Promise<ExecutionResult<string>> { | ||
return executeWorkerFile(path.join(__dirname, 'plainExec.worker.js'), { | ||
file, | ||
args, | ||
options, | ||
}); | ||
} |
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,16 @@ | ||
import { parentPort, workerData } from 'worker_threads'; | ||
import { _workerShellExecImpl } from './workerRawProcessApis'; | ||
|
||
_workerShellExecImpl(workerData.command, workerData.options, workerData.defaultEnv) | ||
.then((res) => { | ||
if (!parentPort) { | ||
throw new Error('Not in a worker thread'); | ||
} | ||
parentPort.postMessage({ res }); | ||
}) | ||
.catch((ex) => { | ||
if (!parentPort) { | ||
throw new Error('Not in a worker thread'); | ||
} | ||
parentPort.postMessage({ ex }); | ||
}); |
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,38 @@ | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
import { ExecOptions, SpawnOptions as ChildProcessSpawnOptions } from 'child_process'; | ||
|
||
export function noop() {} | ||
export interface IDisposable { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
dispose(): void | undefined | Promise<void>; | ||
} | ||
export type EnvironmentVariables = Record<string, string | undefined>; | ||
export class StdErrError extends Error { | ||
// eslint-disable-next-line @typescript-eslint/no-useless-constructor | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} | ||
|
||
export type SpawnOptions = ChildProcessSpawnOptions & { | ||
encoding?: string; | ||
// /** | ||
// * Can't use `CancellationToken` here as it comes from vscode which is not available in worker threads. | ||
// */ | ||
// token?: CancellationToken; | ||
mergeStdOutErr?: boolean; | ||
throwOnStdErr?: boolean; | ||
extraVariables?: NodeJS.ProcessEnv; | ||
// /** | ||
// * Can't use `OutputChannel` here as it comes from vscode which is not available in worker threads. | ||
// */ | ||
// outputChannel?: OutputChannel; | ||
stdinStr?: string; | ||
}; | ||
export type ShellOptions = ExecOptions & { throwOnStdErr?: boolean }; | ||
|
||
export type ExecutionResult<T extends string | Buffer> = { | ||
stdout: T; | ||
stderr?: T; | ||
}; |
Oops, something went wrong.