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QAAO,GAAY,EAAS,OAAS,iBAAA,YAAY,WAAa,EAAS,OAAS,iBAAA,YAAY,YACzG,SAAQ,GAAY,EAAS,YAE5B,EAIF,KAAK,gCAAkC,WAAW,KAAK,wBAAwB,KAAK,KAAM,GAAO,iBAAA,eAAe,aAAa,GAE7H,KAAK,wBAAwB,EAE/B,CAEQ,uBAAA,CAAwB,GAC/B,IAAI,EAAe,KAAK,UAAU,MAAK,GAAU,EAAS,OAAS,iBAAA,YAAY,YAC3E,GAWH,EAAa,SAAS,GACtB,KAAK,qBAAuB,SAAS,EAAa,gBAXlD,KAAK,oBAAsB,aAAY,KACtC,EAAe,KAAK,UAAU,MAAK,GAAU,EAAS,OAAS,iBAAA,YAAY,YACxE,IACF,EAAa,SAAS,GACtB,KAAK,qBAAuB,SAAS,EAAa,eAClD,cAAc,KAAK,qBACpB,GACE,GAML,CAEA,IAAA,CAAK,GAAiB,GACrB,cAAc,KAAK,qBACnB,KAAK,UAAU,SAAQ,GAAY,EAAS,KAAK,KACjD,KAAK,UAAY,EAClB,CAOM,WAAA,CAAY,G,iDACjB,OAAO,eAAA,YAAY,YAAY,EAChC,G,CAMA,QAAA,CAAS,GAAqB,CAO9B,OAAA,CAAQ,GAAe,CAOvB,QAAA,CAAS,GAAe,CAUxB,OAAA,CAAQ,GAA8B,CAEtC,cAAA,CAAe,GAAmB,EA1InC,QAAA","file":"/npm/arepl-backend@4.0.0/pythonExecutors.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/arepl-backend@4.0.0/pythonExecutors.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { Options, PythonShell } from \"python-shell\";\nimport { ExecArgs, PythonExecutor, PythonResult, PythonState } from \"./pythonExecutor\";\n\nexport * from './pythonExecutor'\n\n/**\n * Starts multiple python executors for running user code. \n * Will manage them for you, so you can treat this class\n * as a single executor.\n */\nexport class PythonExecutors {\n\tprivate executors: PythonExecutor[] = []\n\tprivate currentExecutorIndex: number = 0\n\tprivate waitForFreeExecutor: NodeJS.Timeout\n\tprivate wait_for_other_runs_to_complete: NodeJS.Timeout\n\n\tconstructor(public options: Options = {}){}\n\n\tstart(numExecutors=3){\n\t\t// we default to three executors, as it should be enough so that there is always\n\t\t// one available to accept incoming code\n\n\t\tif(this.executors.length != 0) throw Error('already started!')\n\n\t\tfor(let i = 0; i < numExecutors; i++){\n\t\t\tconsole.log('starting executor ' + i.toString())\n\t\t\tconst pyExecutor = new PythonExecutor(this.options)\n\t\t\tpyExecutor.start(()=>{})\n\t\t\tpyExecutor.evaluatorName = i.toString()\n\t\t\tpyExecutor.onResult = result => {\n\t\t\t\t// Other executor may send a result right before it dies\n\t\t\t\t// So we use this function to only capture result from active executor\n\t\t\t\tif(i == this.currentExecutorIndex) this.onResult(result)\n\t\t\t}\n\t\t\tpyExecutor.onPrint = print => {\n\t\t\t\tif(i == this.currentExecutorIndex) this.onPrint(print)\n\t\t\t}\n\t\t\tpyExecutor.onStderr = stderr => {\n\t\t\t\tif(i == this.currentExecutorIndex) this.onStderr(stderr)\n\t\t\t}\n\t\t\tpyExecutor.pyshell.on('error', this.onError)\n\t\t\tpyExecutor.pyshell.childProcess.on('exit', exitCode => {\n\t\t\t\tif(exitCode != 0) this.onAbnormalExit(exitCode)\n\t\t\t})\n\t\t\tthis.executors.push(pyExecutor)\n\t\t}\n\t}\n\n\t/**\n\t * Sends code to the current executor. \n\t * If current executor is busy, nothing happens\n\t */\n\texecCodeCurrent(code: ExecArgs){\n\t\tthis.executors[this.currentExecutorIndex].execCode(code)\n\t}\n\n\t/**\n\t * sends code to a free executor to be executed\n\t * Side-effect: restarts dirty executors\n\t */\n\texecCode(code: ExecArgs){\n\t\t// old code is now irrelevant, if we are still waiting to send old code\n\t\t// we should stop waiting\n\t\tclearInterval(this.waitForFreeExecutor)\n\t\t// this timeout should definitely not still be going on, but we clear it just in case\n\t\tclearTimeout(this.wait_for_other_runs_to_complete)\n\n\t\tlet last_run_still_executing = false\n\t\tif(this.executors.some(executor => executor.state == PythonState.Executing)){\n\t\t\tlast_run_still_executing = true\n\t\t}\n\t\t// executors running old code are now irrelevant, restart them\n\t\tthis.executors.filter(executor => executor.state == PythonState.Executing || executor.state == PythonState.DirtyFree)\n\t\t\t.forEach(executor => executor.restart())\n\n\t\tif(last_run_still_executing){\n\t\t\t// wait for last run to complete\n\t\t\t// we don't want to run two programs at once\n\t\t\t// which could cause a race condition\n\t\t\tthis.wait_for_other_runs_to_complete = setTimeout(this.exec_when_free_executor.bind(this, code), PythonExecutor.GRACE_PERIOD+5)\n\t\t} else{\n\t\t\tthis.exec_when_free_executor(code)\n\t\t}\n\t}\n\n\tprivate exec_when_free_executor(code: ExecArgs){\n\t\tlet freeExecutor = this.executors.find(executor=>executor.state == PythonState.FreshFree)\n\t\tif(!freeExecutor){\n\t\t\tthis.waitForFreeExecutor = setInterval(()=>{\n\t\t\t\tfreeExecutor = this.executors.find(executor=>executor.state == PythonState.FreshFree)\n\t\t\t\tif(freeExecutor){\n\t\t\t\t\tfreeExecutor.execCode(code)\n\t\t\t\t\tthis.currentExecutorIndex = parseInt(freeExecutor.evaluatorName)\n\t\t\t\t\tclearInterval(this.waitForFreeExecutor)\n\t\t\t\t}\n\t\t\t}, 60)\n\t\t}\n\t\telse{\n\t\t\tfreeExecutor.execCode(code)\n\t\t\tthis.currentExecutorIndex = parseInt(freeExecutor.evaluatorName)\n\t\t}\n\t}\n\n\tstop(kill_immediately=false){\n\t\tclearInterval(this.waitForFreeExecutor)\n\t\tthis.executors.forEach(executor => executor.stop(kill_immediately))\n\t\tthis.executors = []\n\t}\n\n\t/**\n\t * checks syntax without executing code\n\t * @param {string} code\n\t * @returns {Promise} rejects w/ stderr if syntax failure\n\t */\n\tasync checkSyntax(code: string) {\n\t\treturn PythonShell.checkSyntax(code);\n\t}\n\n\t/**\n\t * Overwrite this with your own handler.\n\t * is called when active executor fails or completes\n\t */\n\tonResult(foo: PythonResult) { }\n\n\t/**\n\t * Overwrite this with your own handler.\n\t * Is called when active executor prints\n\t * @param {string} foo\n\t */\n\tonPrint(foo: string) { }\n\n\t/**\n\t * Overwrite this with your own handler. \n\t * Is called when active executor logs stderr\n\t * @param {string} foo\n\t */\n\tonStderr(foo: string) { }\n\n\t/**\n\t * Overwrite this with your own handler. \n\t * Is called when there is a Node.JS error event with the python process\n\t *  The 'error' event is emitted whenever:\n\t\t\tThe process could not be spawned, or\n\t\t\tThe process could not be killed, or\n\t\t\tSending a message to the child process failed.\n\t */\n\tonError(err: NodeJS.ErrnoException) { }\n\n\tonAbnormalExit(exitCode: number) {}\n\n\t/**\n\t * delays execution of function by ms milliseconds, resetting clock every time it is called\n\t * Useful for real-time execution so execCode doesn't get called too often\n\t * thanks to https://stackoverflow.com/a/1909508/6629672\n\t */\n\tdebounce = (function () {\n\t\tlet timer: any = 0;\n\t\treturn function (callback, ms: number, ...args: any[]) {\n\t\t\tclearTimeout(timer);\n\t\t\ttimer = setTimeout(callback, ms, args);\n\t\t};\n\t})();\n}"]}