def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" # lock thread-protected stuff self.thread_ready.acquire() # Install sigint handler try: signal.signal(signal.SIGINT, sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass if self._kill: print >> genutils.Term.cout, 'Closing threads...', genutils.Term.cout.flush() for tokill in self.on_kill: tokill() print >> genutils.Term.cout, 'Done.' # Run pending code by calling parent class if self.code_to_run is not None: self.thread_ready.notify() InteractiveShell.runcode(self, self.code_to_run) # We're done with thread-protected variables self.thread_ready.release() # This MUST return true for gtk threading to work return True
def runsource(self, source, filename="<input>", symbol="single"): """Compile and run some source in the interpreter. Modified version of code.py's runsource(), to handle threading issues. See the original for full docstring details.""" # If Ctrl-C was typed, we reset the flag and return right away if shellglobals.KBINT: shellglobals.KBINT = False return False if self._kill: # can't queue new code if we are being killed return True try: code = self.compile(source, filename, symbol) except (OverflowError, SyntaxError, ValueError): # Case 1 self.showsyntaxerror(filename) return False if code is None: # Case 2 return True # shortcut - if we are in worker thread, or the worker thread is not running, # execute directly (to allow recursion and prevent deadlock if code is run early # in IPython construction) if (not self.reactor_started or (self.worker_ident is None and not self.first_run) or self.worker_ident == thread.get_ident() or shellglobals.run_in_frontend(source)): InteractiveShell.runcode(self, code) return # Case 3 # Store code in queue, so the execution thread can handle it. self.first_run = False completed_ev, received_ev = threading.Event(), threading.Event() self.code_queue.put((code, completed_ev, received_ev)) reactor.callLater(0.0, self.runcode) received_ev.wait(5) if not received_ev.isSet(): # the mainloop is dead, start executing code directly print "Warning: Timeout for mainloop thread exceeded" print "switching to nonthreaded mode (until mainloop wakes up again)" self.worker_ident = None else: completed_ev.wait() return False
def runsource(self, source, filename="<input>", symbol="single"): """Compile and run some source in the interpreter. Modified version of code.py's runsource(), to handle threading issues. See the original for full docstring details.""" # If Ctrl-C was typed, we reset the flag and return right away if shellglobals.KBINT: shellglobals.KBINT = False return False if self._kill: # can't queue new code if we are being killed return True try: code = self.compile(source, filename, symbol) except (OverflowError, SyntaxError, ValueError): # Case 1 self.showsyntaxerror(filename) return False if code is None: # Case 2 return True # shortcut - if we are in worker thread, or the worker thread is not running, # execute directly (to allow recursion and prevent deadlock if code is run early # in IPython construction) if (not self.reactor_started or (self.worker_ident is None and not self.first_run) or self.worker_ident == thread.get_ident() or shellglobals.run_in_frontend(source)): InteractiveShell.runcode(self,code) return # Case 3 # Store code in queue, so the execution thread can handle it. self.first_run = False completed_ev, received_ev = threading.Event(), threading.Event() self.code_queue.put((code,completed_ev, received_ev)) reactor.callLater(0.0,self.runcode) received_ev.wait(5) if not received_ev.isSet(): # the mainloop is dead, start executing code directly print "Warning: Timeout for mainloop thread exceeded" print "switching to nonthreaded mode (until mainloop wakes up again)" self.worker_ident = None else: completed_ev.wait() return False
def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" global CODE_RUN # Exceptions need to be raised differently depending on which thread is # active CODE_RUN = True # lock thread-protected stuff got_lock = self.thread_ready.acquire(False) if self._kill: print >> Term.cout, 'Closing threads...', Term.cout.flush() for tokill in self.on_kill: tokill() print >> Term.cout, 'Done.' # Install sigint handler. We do it every time to ensure that if user # code modifies it, we restore our own handling. try: signal(SIGINT, sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass # Flush queue of pending code by calling the run methood of the parent # class with all items which may be in the queue. while 1: try: code_to_run = self.code_queue.get_nowait() except Queue.Empty: break if got_lock: self.thread_ready.notify() InteractiveShell.runcode(self, code_to_run) else: break # We're done with thread-protected variables if got_lock: self.thread_ready.release() # We're done... CODE_RUN = False # This MUST return true for gtk threading to work return True
def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" # lock thread-protected stuff got_lock = self.thread_ready.acquire(False) # Install sigint handler try: signal.signal(signal.SIGINT, sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass if self._kill: print >> Term.cout, 'Closing threads...', Term.cout.flush() for tokill in self.on_kill: tokill() print >> Term.cout, 'Done.' # Flush queue of pending code by calling the run methood of the parent # class with all items which may be in the queue. while 1: try: code_to_run = self.code_queue.get_nowait() except Queue.Empty: break if got_lock: self.thread_ready.notify() InteractiveShell.runcode(self, code_to_run) else: break # We're done with thread-protected variables if got_lock: self.thread_ready.release() # This MUST return true for gtk threading to work return True
def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" global CODE_RUN # we are in worker thread, stash out the id for runsource() self.worker_ident = thread.get_ident() if self._kill: print >>Term.cout, 'Closing threads...', Term.cout.flush() for tokill in self.on_kill: tokill() print >>Term.cout, 'Done.' # allow kill() to return self._kill.set() return True # Install sigint handler. We do it every time to ensure that if user # code modifies it, we restore our own handling. try: signal(SIGINT,sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass # Flush queue of pending code by calling the run methood of the parent # class with all items which may be in the queue. code_to_run = None while 1: try: code_to_run, completed_ev, received_ev = self.code_queue.get_nowait() except Queue.Empty: break received_ev.set() # Exceptions need to be raised differently depending on which # thread is active. This convoluted try/except is only there to # protect against asynchronous exceptions, to ensure that a KBINT # at the wrong time doesn't deadlock everything. The global # CODE_TO_RUN is set to true/false as close as possible to the # runcode() call, so that the KBINT handler is correctly informed. try: try: CODE_RUN = True InteractiveShell.runcode(self,code_to_run) except KeyboardInterrupt: print "Keyboard interrupted in mainloop" while not self.code_queue.empty(): code, ev1,ev2 = self.code_queue.get_nowait() ev1.set() ev2.set() break finally: CODE_RUN = False # allow runsource() return from wait completed_ev.set() # This MUST return true for gtk threading to work return True
def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" global CODE_RUN # lock thread-protected stuff got_lock = self.thread_ready.acquire() if self._kill: print >>Term.cout, 'Closing threads...', Term.cout.flush() for tokill in self.on_kill: tokill() print >>Term.cout, 'Done.' # Install sigint handler. We do it every time to ensure that if user # code modifies it, we restore our own handling. try: signal(SIGINT,sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass # Flush queue of pending code by calling the run methood of the parent # class with all items which may be in the queue. code_to_run = None while 1: try: code_to_run = self.code_queue.get_nowait() except Queue.Empty: break # Exceptions need to be raised differently depending on which # thread is active. This convoluted try/except is only there to # protect against asynchronous exceptions, to ensure that a KBINT # at the wrong time doesn't deadlock everything. The global # CODE_TO_RUN is set to true/false as close as possible to the # runcode() call, so that the KBINT handler is correctly informed. try: try: CODE_RUN = True InteractiveShell.runcode(self,code_to_run) except KeyboardInterrupt: print "Keyboard interrupted in mainloop" while not self.code_queue.empty(): self.code_queue.get_nowait() break finally: if got_lock: CODE_RUN = False # We're done with thread-protected variables if code_to_run is not None: self.thread_ready.notify() self.thread_ready.release() # We're done... CODE_RUN = False # This MUST return true for gtk threading to work return True
def runcode(self): """Execute a code object. Multithreaded wrapper around IPython's runcode().""" global CODE_RUN # lock thread-protected stuff got_lock = self.thread_ready.acquire() if self._kill: print >> Term.cout, 'Closing threads...', Term.cout.flush() for tokill in self.on_kill: tokill() print >> Term.cout, 'Done.' # Install sigint handler. We do it every time to ensure that if user # code modifies it, we restore our own handling. try: signal(SIGINT, sigint_handler) except SystemError: # This happens under Windows, which seems to have all sorts # of problems with signal handling. Oh well... pass # Flush queue of pending code by calling the run methood of the parent # class with all items which may be in the queue. code_to_run = None while 1: try: code_to_run = self.code_queue.get_nowait() except Queue.Empty: break # Exceptions need to be raised differently depending on which # thread is active. This convoluted try/except is only there to # protect against asynchronous exceptions, to ensure that a KBINT # at the wrong time doesn't deadlock everything. The global # CODE_TO_RUN is set to true/false as close as possible to the # runcode() call, so that the KBINT handler is correctly informed. try: try: CODE_RUN = True InteractiveShell.runcode(self, code_to_run) except KeyboardInterrupt: print "Keyboard interrupted in mainloop" while not self.code_queue.empty(): self.code_queue.get_nowait() break finally: if got_lock: CODE_RUN = False # We're done with thread-protected variables if code_to_run is not None: self.thread_ready.notify() self.thread_ready.release() # We're done... CODE_RUN = False # This MUST return true for gtk threading to work return True