federer
Version:
Experiments in asynchronous federated learning and decentralized learning
78 lines • 2.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Process = void 0;
const tslib_1 = require("tslib");
const assert = require("assert");
const nano = tslib_1.__importStar(require("nanoevents"));
/**
* A `Process` represents a process that is managed by the coordinator. This
* class is used to abstract over the exact location of the process, which could
* be on the same thread, on the same machine, or remote.
*
* @typeParam StartOptions - The start options of the process
* @typeParam Socket - The type of the the socket
*/
class Process {
constructor(socket) {
this.socket = socket;
this.events = nano.createNanoEvents();
this.stoppedPromise = new Promise((resolve) => {
this.resolveStopped = resolve;
});
this.readyPromise = new Promise((resolve) => socket.once("ready", () => {
this.events.emit("ready");
resolve();
}));
this.startedPromise = new Promise((resolve) => {
this.resolveStarted = resolve;
});
socket.on("started", () => {
this.events.emit("started");
assert(this.resolveStarted !== undefined);
this.resolveStarted();
// Reset stoppedPromise for next time we get a "stopped"
this.stoppedPromise = new Promise((resolve) => {
this.resolveStopped = resolve;
});
});
this.stoppedPromise = new Promise((resolve) => {
this.resolveStopped = resolve;
});
socket.on("stopped", () => {
this.events.emit("stopped");
assert(this.resolveStopped !== undefined);
this.resolveStopped();
// Reset startedPromise for next time we get a "started"
this.startedPromise = new Promise((resolve) => {
this.resolveStarted = resolve;
});
});
this.killedPromise = new Promise((resolve) => this.socket.once("killed", () => {
this.events.emit("exited", "Killed by 'kill' message");
resolve();
}));
}
ready() {
return this.readyPromise;
}
start(options) {
this.socket.emit("start", options);
}
started() {
return this.startedPromise;
}
stop() {
this.socket.emit("stop");
}
stopped() {
return this.stoppedPromise;
}
kill() {
this.socket.emit("kill");
}
exited() {
return this.killedPromise;
}
}
exports.Process = Process;
//# sourceMappingURL=Process.js.map