Python twisted.internet.defer.DeferredLock() Examples

The following are 18 code examples of twisted.internet.defer.DeferredLock(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.internet.defer , or try the search function .
Example #1
Source File: soledad.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def cleanup_deferred_locks():
    """
    Need to use this from within trial to cleanup the reactor before
    each run.
    """
    SoledadDocumentWrapper._k_locks = defaultdict(defer.DeferredLock) 
Example #2
Source File: test_boot_images.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_calling_importer_issues_rpc_calls_to_clusters(self):
        # Some clusters that we'll ask to import resources.
        rack_1 = factory.make_RackController()
        rack_2 = factory.make_RackController()

        # Connect only cluster #1.
        rack_1_conn = self.rpc.makeCluster(rack_1, ImportBootImages)
        rack_1_conn.ImportBootImages.return_value = succeed({})

        # Do the import.
        importer = RackControllersImporter.new(
            [rack_1.system_id, rack_2.system_id]
        )
        results = importer(lock=DeferredLock()).wait(5)

        # The results are a list (it's from a DeferredList).
        self.assertThat(
            results,
            MatchesListwise(
                (
                    # Success when calling rack_1.
                    Equals((True, {})),
                    # Failure when calling rack_1: no connection.
                    MatchesListwise(
                        (
                            Is(False),
                            MatchesAll(
                                IsInstance(Failure),
                                MatchesStructure(
                                    value=IsInstance(NoConnectionsAvailable)
                                ),
                            ),
                        )
                    ),
                )
            ),
        ) 
Example #3
Source File: runtest.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def _isThreadpoolQuiet(self, pool):
        """Is the given threadpool quiet, i.e. not in use?

        This can handle MAAS's custom threadpools as well as Twisted's default
        implementation.
        """
        lock = getattr(pool, "lock", None)
        if isinstance(lock, defer.DeferredLock):
            return not lock.locked
        elif isinstance(lock, defer.DeferredSemaphore):
            return lock.tokens == lock.limit
        else:
            return len(pool.working) == 0 
Example #4
Source File: test_defer.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_cancelLockBeforeAcquired(self):
        """
        When canceling a L{Deferred} from a L{DeferredLock} that does not
        yet have the lock (i.e., the L{Deferred} has not fired), the cancel
        should cause a L{defer.CancelledError} failure.
        """
        lock = defer.DeferredLock()
        lock.acquire()
        d = lock.acquire()
        self.assertFailure(d, defer.CancelledError)
        d.cancel() 
Example #5
Source File: test_defer.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_cancelLockAfterAcquired(self):
        """
        When canceling a L{Deferred} from a L{DeferredLock} that already
        has the lock, the cancel should have no effect.
        """
        def _failOnErrback(_):
            self.fail("Unexpected errback call!")
        lock = defer.DeferredLock()
        d = lock.acquire()
        d.addErrback(_failOnErrback)
        d.cancel() 
Example #6
Source File: test_defer.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_cancelLockBeforeAcquired(self):
        """
        When canceling a L{Deferred} from a L{DeferredLock} that does not
        yet have the lock (i.e., the L{Deferred} has not fired), the cancel
        should cause a L{defer.CancelledError} failure.
        """
        lock = defer.DeferredLock()
        lock.acquire()
        d = lock.acquire()
        d.cancel()
        self.assertImmediateFailure(d, defer.CancelledError) 
Example #7
Source File: test_defer.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_cancelLockAfterAcquired(self):
        """
        When canceling a L{Deferred} from a L{DeferredLock} that already
        has the lock, the cancel should have no effect.
        """
        def _failOnErrback(_):
            self.fail("Unexpected errback call!")
        lock = defer.DeferredLock()
        d = lock.acquire()
        d.addErrback(_failOnErrback)
        d.cancel() 
Example #8
Source File: protocol.py    From txamqp with Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        AMQClient.__init__(self, *args, **kwargs)

        if self.check_0_8():
            self.replyToField = "reply to"
        else:
            self.replyToField = "reply-to"

        self.thriftBasicReturnQueueLock = defer.DeferredLock()
        self.thriftBasicReturnQueues = {} 
Example #9
Source File: test_defer.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_cancelLockBeforeAcquired(self):
        """
        When canceling a L{Deferred} from a L{DeferredLock} that does not
        yet have the lock (i.e., the L{Deferred} has not fired), the cancel
        should cause a L{defer.CancelledError} failure.
        """
        lock = defer.DeferredLock()
        lock.acquire()
        d = lock.acquire()
        d.cancel()
        self.assertImmediateFailure(d, defer.CancelledError) 
Example #10
Source File: test_defer.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_cancelLockAfterAcquired(self):
        """
        When canceling a L{Deferred} from a L{DeferredLock} that already
        has the lock, the cancel should have no effect.
        """
        def _failOnErrback(_):
            self.fail("Unexpected errback call!")
        lock = defer.DeferredLock()
        d = lock.acquire()
        d.addErrback(_failOnErrback)
        d.cancel() 
Example #11
Source File: soledad.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, doc_id=None, future_doc_id=None, **kwargs):
        self._doc_id = doc_id
        self._future_doc_id = future_doc_id
        self._lock = defer.DeferredLock()
        super(SoledadDocumentWrapper, self).__init__(**kwargs) 
Example #12
Source File: soledad.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def _get_klass_lock(cls):
        """
        Get a DeferredLock that is unique for this subclass name.
        Used to lock the access to indexes in the `get_or_create` call
        for a particular DocumentWrapper.
        """
        return cls._k_locks[cls.__name__] 
Example #13
Source File: protocol.py    From txamqp with Apache License 2.0 4 votes vote down vote up
def __init__(self, delegate, vhost, spec, heartbeat=0, clock=None, insist=False):
        FrameReceiver.__init__(self, spec)
        self.delegate = delegate

        # XXX Cyclic dependency
        self.delegate.client = self

        self.vhost = vhost

        self.channelFactory = type("Channel%s" % self.spec.klass.__name__,
                                   (self.channelClass, self.spec.klass), {})
        self.channels = {}
        self.channelLock = defer.DeferredLock()

        self.outgoing = defer.DeferredQueue()
        self.work = defer.DeferredQueue()

        self.started = TwistedEvent()
        self.disconnected = TwistedEvent()  # Fired upon connection shutdown
        self.closed = False

        self.queueLock = defer.DeferredLock()
        self.basic_return_queue = TimeoutDeferredQueue()

        self.queues = {}

        self.outgoing.get().addCallback(self.writer)
        self.work.get().addCallback(self.worker)
        self.heartbeatInterval = heartbeat
        self.insist = insist
        if clock is None:
            from twisted.internet import reactor
            clock = reactor
        self.clock = clock
        if self.heartbeatInterval > 0:
            self.checkHB = self.clock.callLater(self.heartbeatInterval *
                                                self.MAX_UNSEEN_HEARTBEAT, self.check_heartbeat)
            self.sendHB = LoopingCall(self.send_heartbeat)
            self.sendHB.clock = self.clock
            d = self.started.wait()
            d.addCallback(lambda _: self.reschedule_send_heartbeat())
            d.addCallback(lambda _: self.reschedule_check_heartbeat())
            # If self.started fails, don't start the heartbeat.
            d.addErrback(lambda _: None) 
Example #14
Source File: test_defer.py    From learn_python3_spider with MIT License 4 votes vote down vote up
def testLock(self):
        lock = defer.DeferredLock()
        lock.acquire().addCallback(self._incr)
        self.assertTrue(lock.locked)
        self.assertEqual(self.counter, 1)

        lock.acquire().addCallback(self._incr)
        self.assertTrue(lock.locked)
        self.assertEqual(self.counter, 1)

        lock.release()
        self.assertTrue(lock.locked)
        self.assertEqual(self.counter, 2)

        lock.release()
        self.assertFalse(lock.locked)
        self.assertEqual(self.counter, 2)

        self.assertRaises(TypeError, lock.run)

        firstUnique = object()
        secondUnique = object()

        controlDeferred = defer.Deferred()
        def helper(self, b):
            self.b = b
            return controlDeferred

        resultDeferred = lock.run(helper, self=self, b=firstUnique)
        self.assertTrue(lock.locked)
        self.assertEqual(self.b, firstUnique)

        resultDeferred.addCallback(lambda x: setattr(self, 'result', x))

        lock.acquire().addCallback(self._incr)
        self.assertTrue(lock.locked)
        self.assertEqual(self.counter, 2)

        controlDeferred.callback(secondUnique)
        self.assertEqual(self.result, secondUnique)
        self.assertTrue(lock.locked)
        self.assertEqual(self.counter, 3)

        d = lock.acquire().addBoth(lambda x: setattr(self, 'result', x))
        d.cancel()
        self.assertEqual(self.result.type, defer.CancelledError)

        lock.release()
        self.assertFalse(lock.locked) 
Example #15
Source File: test_defer.py    From python-for-android with Apache License 2.0 4 votes vote down vote up
def testLock(self):
        lock = defer.DeferredLock()
        lock.acquire().addCallback(self._incr)
        self.failUnless(lock.locked)
        self.assertEquals(self.counter, 1)

        lock.acquire().addCallback(self._incr)
        self.failUnless(lock.locked)
        self.assertEquals(self.counter, 1)

        lock.release()
        self.failUnless(lock.locked)
        self.assertEquals(self.counter, 2)

        lock.release()
        self.failIf(lock.locked)
        self.assertEquals(self.counter, 2)

        self.assertRaises(TypeError, lock.run)

        firstUnique = object()
        secondUnique = object()

        controlDeferred = defer.Deferred()
        def helper(self, b):
            self.b = b
            return controlDeferred

        resultDeferred = lock.run(helper, self=self, b=firstUnique)
        self.failUnless(lock.locked)
        self.assertEquals(self.b, firstUnique)

        resultDeferred.addCallback(lambda x: setattr(self, 'result', x))

        lock.acquire().addCallback(self._incr)
        self.failUnless(lock.locked)
        self.assertEquals(self.counter, 2)

        controlDeferred.callback(secondUnique)
        self.assertEquals(self.result, secondUnique)
        self.failUnless(lock.locked)
        self.assertEquals(self.counter, 3)

        d = lock.acquire().addBoth(lambda x: setattr(self, 'result', x))
        d.cancel()
        self.assertEquals(self.result.type, defer.CancelledError)

        lock.release()
        self.failIf(lock.locked) 
Example #16
Source File: test_defer.py    From Safejumper-for-Desktop with GNU General Public License v2.0 4 votes vote down vote up
def testLock(self):
        lock = defer.DeferredLock()
        lock.acquire().addCallback(self._incr)
        self.assertTrue(lock.locked)
        self.assertEqual(self.counter, 1)

        lock.acquire().addCallback(self._incr)
        self.assertTrue(lock.locked)
        self.assertEqual(self.counter, 1)

        lock.release()
        self.assertTrue(lock.locked)
        self.assertEqual(self.counter, 2)

        lock.release()
        self.assertFalse(lock.locked)
        self.assertEqual(self.counter, 2)

        self.assertRaises(TypeError, lock.run)

        firstUnique = object()
        secondUnique = object()

        controlDeferred = defer.Deferred()
        def helper(self, b):
            self.b = b
            return controlDeferred

        resultDeferred = lock.run(helper, self=self, b=firstUnique)
        self.assertTrue(lock.locked)
        self.assertEqual(self.b, firstUnique)

        resultDeferred.addCallback(lambda x: setattr(self, 'result', x))

        lock.acquire().addCallback(self._incr)
        self.assertTrue(lock.locked)
        self.assertEqual(self.counter, 2)

        controlDeferred.callback(secondUnique)
        self.assertEqual(self.result, secondUnique)
        self.assertTrue(lock.locked)
        self.assertEqual(self.counter, 3)

        d = lock.acquire().addBoth(lambda x: setattr(self, 'result', x))
        d.cancel()
        self.assertEqual(self.result.type, defer.CancelledError)

        lock.release()
        self.assertFalse(lock.locked) 
Example #17
Source File: test_defer.py    From BitTorrent with GNU General Public License v3.0 4 votes vote down vote up
def testLock(self):
        lock = defer.DeferredLock()
        lock.acquire().addCallback(self._incr)
        self.failUnless(lock.locked)
        self.assertEquals(self.counter, 1)

        lock.acquire().addCallback(self._incr)
        self.failUnless(lock.locked)
        self.assertEquals(self.counter, 1)

        lock.release()
        self.failUnless(lock.locked)
        self.assertEquals(self.counter, 2)

        lock.release()
        self.failIf(lock.locked)
        self.assertEquals(self.counter, 2)

        self.assertRaises(TypeError, lock.run)

        firstUnique = object()
        secondUnique = object()

        controlDeferred = defer.Deferred()
        def helper(self, b):
            self.b = b
            return controlDeferred

        resultDeferred = lock.run(helper, self=self, b=firstUnique)
        self.failUnless(lock.locked)
        self.assertEquals(self.b, firstUnique)

        resultDeferred.addCallback(lambda x: setattr(self, 'result', x))

        lock.acquire().addCallback(self._incr)
        self.failUnless(lock.locked)
        self.assertEquals(self.counter, 2)

        controlDeferred.callback(secondUnique)
        self.assertEquals(self.result, secondUnique)
        self.failUnless(lock.locked)
        self.assertEquals(self.counter, 3)

        lock.release()
        self.failIf(lock.locked) 
Example #18
Source File: llvmgitpoller.py    From llvm-zorg with Apache License 2.0 4 votes vote down vote up
def __init__(self, repourl=_repourl, branch=_branch,
                 workdir=None, pollInterval=10*60,
                 gitbin='git', usetimestamps=True,
                 category=None, project=None,
                 pollinterval=-2, fetch_refspec=None,
                 encoding='utf-8', projects=None):

        self.cleanRe = re.compile(r"Require(?:s?)\s*.*\s*clean build", re.IGNORECASE + re.MULTILINE)
        self.cleanCfg = re.compile(r"(CMakeLists\.txt$|\.cmake$|\.cmake\.in$)")

        # projects is a list of projects to watch or None to watch all.
        if projects:
            if isinstance(projects, str) or isinstance(projects, tuple):
                projects = [projects]
            assert isinstance(projects, list)
            assert len(projects) > 0

            # Each project to watch is a string (project name) or a tuple
            # (project name, branch) like ('llvm', 'branches/release_30').
            # But we want it always to be a tuple, so we convert a project
            # name string to a tuple (project, 'master').
            self.projects = set()
            for project in projects:
                if isinstance(project, str):
                    project = (project, branch)

                assert isinstance(project, tuple)
                self.projects.add(project)

        # for backward compatibility; the parameter used to be spelled with 'i'
        if pollinterval != -2:
            pollInterval = pollinterval
        if project is None: project = ''

        self.repourl = repourl
        self.branch = branch
        self.pollInterval = pollInterval
        self.fetch_refspec = fetch_refspec
        self.encoding = encoding
        self.lastChange = time.time()
        self.lastPoll = time.time()
        self.gitbin = gitbin
        self.workdir = workdir
        self.usetimestamps = usetimestamps
        self.category = category
        self.project = project
        self.changeCount = 0
        self.commitInfo  = {}
        self.initLock = defer.DeferredLock()

        if self.workdir == None:
            self.workdir = tempfile.gettempdir() + '/gitpoller_work'
            log.msg("WARNING: LLVMGitPoller using deprecated temporary workdir " +
                    "'%s'; consider setting workdir=" % self.workdir)