@himalaya-quant/synapse
Version:
A lightweight TypeScript utility to spawn and interact with Python modules from Node.js with a native, message-based protocol over stdin/stdout.
93 lines • 3.81 kB
TypeScript
export declare class InstanceManger {
private readonly inputsQueue;
private readonly instanceOutputs$;
private readonly instanceLogs$;
private readonly instanceInputs$;
private messageBuffer;
private instance;
private currentInputResolver;
private instanceInputStreamSubscription;
private instanceOutputStreamSubscription;
/**
* Subscription to the instance logs.
* If you want your script logs to be seen by Synapse, in your python script
* always print to the stderr like:
*
* ```py
* import sys
*
* print("my log", file=sys.stderr)
* ```
*
* every log written this way will pass through this stream.
*
* @returns The observable to which you can subscribe to access
* the logs stream.
*/
get instanceLogs(): import("rxjs").Observable<string>;
/**
* Calls the spawned python instance with the given input.
* Throws if the instance has not been spawned first, or if the script sends
* an error response.
*
* To send an error response, just send the usual message from the script,
* but passing a a dictionary with an "error" key and the error message
* as value. Eg: {"error": "my error message"}
*
* @param input Any simple JSON structure will be accepted.
* For more details see: https://msgpack.org/
* @param forceJSONParse Forcefully tries to parse the result. If it
* fails, will return the payload as it is.
*
* @returns The result returned from your python script.
* @throws {Error} If the instance has not been spawned or an error response
* is sent back from the python script
*/
call<ResultType = any>(input: any, forceJSONParse?: boolean): Promise<ResultType>;
/**
* Spawns a new python script instance and keeps it alive until dispose is
* called. After the spawning you can safely start sending messages to it.
* Throws if there's an error during the spawning process.
*
* What it does:
* - Postfixes the extension .py to the entrypoint if missing
* - Ensures that the paths are correct and the requirements.txt exists
* - Creates a dedicated Python virtual environment
* - Installs dependencies via requirements.txt
* - Spawns Python script as subprocess
* - Reuses instance until explicit disposal avoiding spawn overhead
*
* @param directory The path pointing to the python module directory.
* @param entrypoint The entrypoint file name.
*
* @returns A promise that resolves once the spawn completes.
* @throws {Error} If there's an error during the spawning process.
*/
spawn(directory: string, entrypoint: string): Promise<void>;
/**
* Disposes the instance, closing the stdin stream, all the subscriptions
* and tries to kill the instance.
* Manages graceful and forceful termination, first tries with a SIGTERM, if
* after 500ms is not killed, will force a SIGKILL.
* Throws if after the SIGKILL the instance is still alive.
*
* After dispose has been called, you will have to call spawn again. Trying
* to send any messages after dispose, will result in an error.
*
* @returns Resolves once the dispose has been done.
* @throws {Error} If after forceful termination the instance is still alive
*/
dispose(): Promise<void>;
private waitForTermination;
private ensureExistsOrThrow;
private createVirtualEnv;
private installDependencies;
private getVirtualEnvPythonInterpreter;
private spawnInstance;
private packAndSend;
private postfixExtension;
private handleChunk;
private openSubscriptions;
private extractResult;
}
//# sourceMappingURL=instance-manager.service.d.ts.map