def test_routine(): @blocking def expensive(t): print 'Thread ',getThreadID(), ' is handling expensive computation ', t if t == 2: raise Hodor() return t + 5 for i in range(10): try: v = (yield expensive(i)) print getThreadID(), 'My expensive operation resulted in ', v except Hodor: print 'Oh noes, processing ',i, ' blew up' import traceback traceback.print_exc() yield Sleep(0.5)
def _assertCorrectThread(self): if threadable.getThreadID() != self._threadId: raise TransactionError( "Tried to rollback a transaction from a different thread.\n" "Make sure that you properly use blockingCallFromThread() and\n" "that you don't add callbacks to Deferreds which get resolved from another thread." )
def test_disconnectedOnError(self): """ If the event handler raises an exception, the event is removed from the reactor and the handler's C{connectionLost} method is called in the I/O thread and the exception is logged. """ reactorThreadID = getThreadID() reactor = self.buildReactor() event = win32event.CreateEvent(None, False, False, None) result = [] finished = Deferred() finished.addBoth(result.append) finished.addBoth(lambda ignored: reactor.stop()) listener = Listener(finished) reactor.addEvent(event, listener, 'brokenOccurred') reactor.callWhenRunning(win32event.SetEvent, event) self.runReactor(reactor) self.assertIsInstance(result[0], Failure) result[0].trap(RuntimeError) self.assertEqual(reactorThreadID, listener.connLostThreadID) self.assertEqual(1, len(self.flushLoggedErrors(RuntimeError)))
def test_addEvent(self): """ When an event which has been added to the reactor is set, the action associated with the event is invoked in the reactor thread. """ reactorThreadID = getThreadID() reactor = self.buildReactor() event = win32event.CreateEvent(None, False, False, None) finished = Deferred() finished.addCallback(lambda ignored: reactor.stop()) listener = Listener(finished) reactor.addEvent(event, listener, 'occurred') reactor.callWhenRunning(win32event.SetEvent, event) self.runReactor(reactor) self.assertTrue(listener.success) self.assertEqual(reactorThreadID, listener.logThreadID) self.assertEqual(reactorThreadID, listener.eventThreadID)
def __init__(self, parent, thread_check=True): # Transactions must be started in db thread unless explicitely permitted if thread_check and threading.current_thread() not in Registry.DBPOOL.threadpool.threads: raise TransactionError("Transaction must only be started in a db pool thread") if parent is None: self._root = self else: self._root = parent._root self._actual_parent = parent self.is_active = True self._threadId = threadable.getThreadID() self._savepoint_seq = 0 if not self._parent.is_active: raise TransactionError("Parent transaction is inactive") Registry.getConfig().txnGuard.txn = self
def __init__(self, parent, thread_check=True): # Transactions must be started in db thread unless explicitely permitted if thread_check and threading.current_thread( ) not in Registry.DBPOOL.threadpool.threads: raise TransactionError( "Transaction must only be started in a db pool thread") if parent is None: self._root = self else: self._root = parent._root self._actual_parent = parent self.is_active = True self._threadId = threadable.getThreadID() self._savepoint_seq = 0 if not self._parent.is_active: raise TransactionError("Parent transaction is inactive") Registry.getConfig().txnGuard.txn = self
def test_disconnectOnReturnValue(self): """ If the event handler returns a value, the event is removed from the reactor and the handler's C{connectionLost} method is called in the I/O thread. """ reactorThreadID = getThreadID() reactor = self.buildReactor() event = win32event.CreateEvent(None, False, False, None) result = [] finished = Deferred() finished.addBoth(result.append) finished.addBoth(lambda ignored: reactor.stop()) listener = Listener(finished) reactor.addEvent(event, listener, 'returnValueOccurred') reactor.callWhenRunning(win32event.SetEvent, event) self.runReactor(reactor) self.assertIsInstance(result[0], Failure) result[0].trap(EnvironmentError) self.assertEqual(reactorThreadID, listener.connLostThreadID)
def echoFromThread(self, line): return "{0}: {1}".format(threadable.getThreadID(), line)
def two(): print getThreadID(), 'running two!' yield Return(5)
def connectionLost(self, reason): self.connLostThreadID = getThreadID() self._finished.errback(reason)
def occurred(self): self.success = True self.eventThreadID = getThreadID() self._finished.callback(None)
def trans2(txn): threadIds.append(threadable.getThreadID()) on_trans1_insert.wait() yield Transaction(name="TEST1").save() barrier2.wait() # wait here to delay commit
def one(): yield Sleep(0.5) print getThreadID(), 'Back in one, calling two' result = (yield two()) print getThreadID(), 'one has resumed: two returned ', result
def expensive(t): print 'Thread ',getThreadID(), ' is handling expensive computation ', t if t == 2: raise Hodor() return t + 5
def trans1(txn): threadIds.append(threadable.getThreadID()) yield Transaction(name="TEST1").save()
def _assertCorrectThread(self): if threadable.getThreadID() != self._threadId: raise TransactionError("Tried to rollback a transaction from a different thread.\n" "Make sure that you properly use blockingCallFromThread() and\n" "that you don't add callbacks to Deferreds which get resolved from another thread.")
def logPrefix(self): self.logThreadID = getThreadID() return 'Listener'
def trans2(txn): threadIds.append(threadable.getThreadID()) yield trans1() yield Transaction(name="TEST2").save() barrier.wait() # wait here to delay commit