def worker_supervisor(self, workers): try: workers = [ worker for worker in workers if worker["stage"].timeout > 0 ] if not workers: return while not self._iter_done: for worker in workers: if (not worker["worker_namespace"].done and worker["worker_namespace"].task_start_time is not None): if (time.time() - worker["worker_namespace"].task_start_time > worker["stage"].timeout): worker["worker_namespace"].task_start_time = None stopit.async_raise(worker["process"].ident, utils.StopThreadException) worker["process"] = worker[ "stage"].worker_constructor( target=worker["stage"].run, args=(worker["index"], worker["worker_namespace"]), ) worker["process"].daemon = True worker["process"].start() time.sleep(0.05) except BaseException as e: self.signal_error(e)
def raise_exception(script, exception): for tid, tobj in threading._active.items(): try: if tobj.script_path == script: stopit.async_raise(tid, exception) break except: continue
def stop(self): if self.process is None: return self.namespace.task_start_time = None if not self.process.is_alive(): return stopit.async_raise( self.process.ident, pypeln_utils.StopThreadException, )
def _stop(id): try: thread = __tasks__[id].__thread__ except KeyError: return for tid, tobj in threading._active.items(): try: if tobj == thread: stopit.async_raise(tid, TaskExit) break except: continue
def stop(self): if self.process is None: return if not self.process.is_alive(): return if isinstance(self.process, multiprocessing.Process): self.process.terminate() else: stopit.async_raise( self.process.ident, pypeln_utils.StopThreadException, ) self.namespace.task_start_time = None
def interruption_loop(): while thread.isAlive(): sys.__stdout__.write(str(Python.shared._isScriptRunning) + "\n") if not Python.shared._isScriptRunning or Python.shared._interrupt: target_tid = 0 for tid, tobj in threading._active.items(): if tobj is thread: found = True target_tid = tid break if Python.shared._interrupt: stopit.async_raise(target_tid, KeyboardInterrupt) elif not Python.shared._isScriptRunning: stopit.async_raise(target_tid, SystemExit) Python.shared._interrupt = False if thread.isAlive: Python.shared._isScriptRunning = True time.sleep(1)
def run_script(path, replMode=False, debug=False, breakpoints=[], runREPL=True): """ Run the script at given path catching exceptions. This function should only be used internally by Pyto. Args: path: The path of the script. replMode: If set to `True`, errors will not be handled. debug: Set to `True` for debugging. breakpoints: Lines to break if debugging. runREPL: Set it to `True` for running the REPL. """ __repl_namespace__[path.split("/")[-1]] = {} __clear_mods__() python = Python.shared python.addScriptToList(path) if PyCallbackHelper is not None: PyCallbackHelper.exception = None is_watch_script = False if path == str(Python.watchScriptURL.path): is_watch_script = True currentDir = "" try: currentDir = str(python.currentWorkingDirectory) except: currentDir = os.path.expanduser(os.path.dirname(path)) try: del os.environ["ps1"] except KeyError: pass try: del os.environ["ps2"] except KeyError: pass sys.argv = [path] for arg in python.args: if arg != "": sys.argv.append(str(arg)) d = os.path.expanduser("~/tmp") filesToRemove = [] try: filesToRemove = [os.path.join(d, f) for f in os.listdir(d)] except: pass try: filesToRemove.remove(d + "/Script.py") except: pass try: filesToRemove.remove(d + "/Watch.py") except: pass for f in filesToRemove: if f.endswith(".repl.py"): continue if f.endswith(".tmp"): continue try: os.remove(f) except PermissionError: pass # Kill the REPL running for this script global __repl_threads__ if path in __repl_threads__: thread = __repl_threads__[path] for tid, tobj in threading._active.items(): if tobj is thread: try: stopit.async_raise(tid, SystemExit) break except: pass del __repl_threads__[path] def run(): def add_signal_handler(s, f): return loop = asyncio.new_event_loop() loop.add_signal_handler = add_signal_handler asyncio.set_event_loop(loop) pip_directory = os.path.expanduser("~/Documents/site-packages") Python.shared.isScriptRunning = True os.chdir(currentDir) try: sys.path.remove(pip_directory) except: pass sys.path.insert(-1, currentDir) sys.path.insert(-1, pip_directory) try: global __script__ spec = importlib.util.spec_from_file_location("__main__", path) __script__ = importlib.util.module_from_spec(spec) sys.modules["__main__"] = __script__ if debug and "widget" not in os.environ: try: console except: import console console.__are_breakpoints_set__ = False console.__breakpoints__ = breakpoints console.__i__ = -1 old_input = input def debugger_input(prompt): try: console except: import console if not console.__are_breakpoints_set__: breakpoints = console.__breakpoints__ console.__i__ += 1 if len(breakpoints) < console.__i__: console.__are_breakpoints_set__ = True return "" try: breakpoints[console.__i__ + 1] except: console.__are_breakpoints_set__ = True return "b " + str(breakpoints[console.__i__]) else: console.__should_inspect__ = True return old_input(prompt) if len(breakpoints) > 0: builtins.input = debugger_input pdb.main(["pdb", path]) builtins.input = old_input loop.close() else: spec.loader.exec_module(__script__) loop.close() return (path, vars(__script__), None) except SystemExit: if PyCallbackHelper is not None: PyCallbackHelper.cancelled = True loop.close() return (path, vars(__script__), SystemExit) except KeyboardInterrupt: if PyCallbackHelper is not None: PyCallbackHelper.cancelled = True loop.close() return (path, vars(__script__), KeyboardInterrupt) except Exception as e: if PyCallbackHelper is not None: PyCallbackHelper.exception = str(e) loop.close() if not __isMainApp__() or replMode: print(traceback.format_exc()) if not replMode: Python.shared.fatalError(traceback.format_exc()) else: exc_type, exc_obj, exc_tb = sys.exc_info() extracts = traceback.extract_tb(exc_tb) count = len(extracts) lineNumber = -1 fileName = path for i, extract in enumerate(extracts): if extract[0] == fileName: lineNumber = extract[1] break count -= 1 if ( type(e) == SyntaxError ): # The last word in a `SyntaxError` exception is the line number lineNumber = [ int(s) for s in (str(e)[:-1]).split() if s.isdigit() ][-1] Python.shared.errorType = exc_type.__name__ Python.shared.errorReason = str(e) for console in ConsoleViewController.objcVisibles: if ( console.editorSplitViewController.editor.document.fileURL.path != path ): continue console.editorSplitViewController.editor.showErrorAtLine( lineNumber ) excepthook(exc_type, exc_obj, exc_tb, -count) try: PyOutputHelper.printError( "", script=threading.current_thread().script_path ) except AttributeError: PyOutputHelper.printError("", script=None) error = traceback.format_exc(limit=-count) if "cv2.error" in error and "!_src.empty()" in error: string = "\nOn Pyto, 'cv2.VideoCapture.read' may return an invalid value the first time. If you are running a loop for capturing a video from the camera, check if the return value is valid before using it. See the 'OpenCV/face_detection.py' example.\n" try: PyOutputHelper.printError( string, script=threading.current_thread().script_path ) except AttributeError: PyOutputHelper.printError(string, script=None) sys.path.remove(currentDir) if debug: pdb.post_mortem(exc_tb) return (path, vars(__script__), e) if __isMainApp__(): EditorViewController.runningLine = 0 ConsoleViewController.enableDoneButton() ReviewHelper.shared.launches = ReviewHelper.shared.launches + 1 ReviewHelper.shared.requestReview() Python.shared.isScriptRunning = True Python.shared._isScriptRunning = True def run_repl(t): global __repl_threads__ Python.shared._isScriptRunning = False Python.shared.isScriptRunning = False Python.shared.removeScriptFromList(path) if path.endswith(".repl.py") or not runREPL: return if type(t) is tuple and len(t) == 3 and not is_watch_script: __repl_threads__[t[0]] = threading.current_thread() __runREPL__(t[0].split("/")[-1], t[1], "") _script = None if "__editor_delegate__" in dir(builtins) and builtins.__editor_delegate__ is not None: delegate = builtins.__editor_delegate__ def _run(): import builtins delegate = builtins.__editor_delegate__ t = run() if type(t) is tuple and len(t) == 3: try: delegate.did_run_script(t[0], t[1], t[2]) except NotImplementedError: run_repl(t) except SystemExit: run_repl(t) except KeyboardInterrupt: run_repl(t) except Exception: traceback.print_tb() try: delegate.run_script(path, _run) except NotImplementedError: run_repl(_run()) else: # Return the script's __dict__ for the Xcode template t = run() if __isMainApp__(): run_repl(t) else: _script = t[1] Python.shared._isScriptRunning = False Python.shared.isScriptRunning = False Python.shared.removeScriptFromList(path) sys.path = list(dict.fromkeys(sys.path)) # I don't remember why 😠if "widget" not in os.environ: import watch watch.__show_ui_if_needed__() __clear_mods__() #time.sleep(0.2) return _script