当前位置: 首页>>代码示例>>Python>>正文


Python ResizableDispatchQueue.put方法代码示例

本文整理汇总了Python中txrdq.rdq.ResizableDispatchQueue.put方法的典型用法代码示例。如果您正苦于以下问题:Python ResizableDispatchQueue.put方法的具体用法?Python ResizableDispatchQueue.put怎么用?Python ResizableDispatchQueue.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在txrdq.rdq.ResizableDispatchQueue的用法示例。


在下文中一共展示了ResizableDispatchQueue.put方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testStopAndCancelWithOneUnderway

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
    def testStopAndCancelWithOneUnderway(self):
        """
        Start a dispatch queue of width 2, and send it 3 jobs. Verify that
        2 of the jobs are underway. Then stop it before they can complete,
        telling it to cancel the underway jobs. The two jobs that were
        underway should both be cancelled and returned by the stop method.
        The first 2 jobs returned should have state CANCELLED, and the
        final one should still be PENDING.
        """
        def ok(result):
            self.fail('Unexpected success!')

        def checkCancel(failure):
            self.assertEqual(failure.value.state, Job.CANCELLED)

        dq = ResizableDispatchQueue(self.slow, 2)
        dq.put(0).addCallbacks(ok, checkCancel)
        dq.put(1).addCallbacks(ok, checkCancel)
        dq.put(2)
        reactor.callLater(0.01, self._testUnderway, dq, set([0, 1]))
        pendingJobs = yield task.deferLater(
            reactor, 0.1, dq.stop, cancelUnderway=True)
        pendingArgs = [p.jobarg for p in pendingJobs]
        self.assertEqual([0, 1, 2], sorted(pendingArgs))
        self.assertEqual(pendingJobs[0].state, Job.CANCELLED)
        self.assertEqual(pendingJobs[1].state, Job.CANCELLED)
        self.assertEqual(pendingJobs[2].state, Job.PENDING)
开发者ID:jcollie,项目名称:txrdq,代码行数:29,代码来源:test_rdq.py

示例2: testReprioritizePending

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
 def testReprioritizePending(self):
     """
     Reprioritize a pending job.
     """
     dq = ResizableDispatchQueue(None, 0)
     dq.put('xxx', 5)
     pending = dq.pending()
     job = pending.pop()
     dq.reprioritize(job, 10)
     self.assertEqual(10, job.priority)
开发者ID:jcollie,项目名称:txrdq,代码行数:12,代码来源:test_rdq.py

示例3: testSetWidthToZeroAfterInitiallyNonZeroThenStop

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
 def testSetWidthToZeroAfterInitiallyNonZeroThenStop(self):
     """
     Make sure that a queue whose width is initially non-zero and which
     is then set to zero width returns all the added jobs when stopped.
     """
     dq = ResizableDispatchQueue(None, 3)
     dq.width = 0
     dq.put('aaa', 5)
     dq.put('bbb', 10)
     remaining = yield dq.stop()
     self.assertEqual(['aaa', 'bbb'], [job.jobarg for job in remaining])
开发者ID:jcollie,项目名称:txrdq,代码行数:13,代码来源:test_rdq.py

示例4: testSetWidthToZeroAfterInitiallyNonZero

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
 def testSetWidthToZeroAfterInitiallyNonZero(self):
     """
     Make sure that a queue whose width is initially non-zero and which
     is then set to zero width does not then begin to process any added
     jobs.
     """
     dq = ResizableDispatchQueue(None, 3)
     dq.width = 0
     dq.put('aaa')
     dq.put('bbb')
     self.assertEqual((0, 2), dq.size())
     return dq.stop()
开发者ID:jcollie,项目名称:txrdq,代码行数:14,代码来源:test_rdq.py

示例5: testCancelRemovesFromQueue

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
    def testCancelRemovesFromQueue(self):
        """
        Cancelling a job should remove it from the queue.
        """
        def eb(failure):
            self.assertEqual(failure.value.state, Job.CANCELLED)

        dq = ResizableDispatchQueue(self.slow, 1)
        dq.put('a', 1)
        deferred = dq.put('b', 2)
        deferred.addErrback(eb)
        deferred.cancel()
        yield task.deferLater(reactor, 0.1, dq.pause)
        pendingJobs = dq.pending()
        self.assertEqual(pendingJobs, [])
开发者ID:jcollie,项目名称:txrdq,代码行数:17,代码来源:test_rdq.py

示例6: testReprioritizeNonPendingJob

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
 def testReprioritizeNonPendingJob(self):
     """
     Reprioritizing a job that is not pending must raise
     C{RuntimeError}.
     """
     dq = ResizableDispatchQueue(lambda x: x, 1)
     job = yield dq.put('a')
     self.assertRaises(RuntimeError, dq.reprioritize, job, 10)
开发者ID:jcollie,项目名称:txrdq,代码行数:10,代码来源:test_rdq.py

示例7: testDeferredFromPutFiresWithCorrectResult

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
 def testDeferredFromPutFiresWithCorrectResult(self):
     """
     Make sure the C{Deferred} we get back from a put to the queue fires
     with the expected result.
     """
     dq = ResizableDispatchQueue(lambda x: 2 * x, width=1)
     job = yield dq.put(6)
     self.assertEqual(job.result, 12)
开发者ID:jcollie,项目名称:txrdq,代码行数:10,代码来源:test_rdq.py

示例8: testClearQueueCancelPending

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
    def testClearQueueCancelPending(self):
        """
        When the queue is cleared with cancelPending=True, the pending jobs
        should fail and receive a job whose state is CANCELLED.
        """
        def ok(result):
            self.fail('Unexpected success!')

        def checkCancel(failure):
            self.assertEqual(failure.value.state, Job.CANCELLED)

        dq = ResizableDispatchQueue(None, 2)
        dq.put(0).addCallbacks(ok, checkCancel)
        dq.put(1).addCallbacks(ok, checkCancel)
        self.assertEqual((0, 2), dq.size())
        dq.clearQueue(cancelPending=True)
        self.assertEqual((0, 0), dq.size())
        yield dq.stop()
开发者ID:jcollie,项目名称:txrdq,代码行数:20,代码来源:test_rdq.py

示例9: testRaiseFailsWithJob

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
    def testRaiseFailsWithJob(self):
        """
        Raising an exception in the job handler should result in a failure
        that contains a L{txrdq.job.Job} value.
        """
        def raiseException(jobarg):
            raise Exception()

        dq = ResizableDispatchQueue(raiseException, 1)
        d = dq.put('Some argument', 1)
        return self.assertFailure(d, Job)
开发者ID:mwilliams3,项目名称:txrdq,代码行数:13,代码来源:test_rdq.py

示例10: testRaiseFailsWithCorrectJobArgument

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
    def testRaiseFailsWithCorrectJobArgument(self):
        """
        Raising an exception in the job handler should result in a failure
        that contains a job with the original job argument.
        """
        result = []

        def raiseException(jobarg):
            raise Exception()

        def extractJobarg(failure):
            result.append(failure.value.jobarg)

        dq = ResizableDispatchQueue(raiseException, 1)
        yield dq.put('Some argument', 1).addErrback(extractJobarg)
        self.assertEqual(['Some argument'], result)
开发者ID:mwilliams3,项目名称:txrdq,代码行数:18,代码来源:test_rdq.py

示例11: AdderCache

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
class AdderCache(DumpingCache):
    def __init__(self, cache, queueWidth, endpoint):
        super(AdderCache, self).__init__()
        self.cache = cache
        self.queueWidth = queueWidth
        self.endpoint = endpoint

    def load(self, cacheFile):
        self.rdq = ResizableDispatchQueue(self._addUser, width=self.queueWidth)
        self.users = super(AdderCache, self).load(cacheFile)
        if self.users is None:
            self.users = {}
            self.setCache(self.users)
        else:
            added = [u for u in self.users.values() if u.state == "added"]
            notAdded = [u for u in self.users.values() if u.state != "added"]
            log.msg("Loaded adder cache: found %d added, %d unadded users" % (len(added), len(notAdded)))
            if self.cache.restoreAddQueue:
                log.msg("Restoring add queue.")
                # Re-queue, in original queue order, any users that are
                # marked as having been added.
                for user in sorted(notAdded, key=attrgetter("queuedAt")):
                    log.msg("Restoring %r (previous state %r)" % (user.screenname, user.state))
                    user.reset()
                    self.rdq.put(user)
                    self.clean = False
            else:
                log.msg("Not restoring formerly queued names.")
                # Drop users that were not added last time.
                for user in notAdded:
                    log.msg("Dropping user %r (in state %r)" % (user.screenname, user.state))
                    del self.users[user.screenname.lower()]
                    self.clean = False

    def __str__(self):
        s = ["%d users in adder cache" % len(self.users)]
        for key in sorted(self.users.keys()):
            s.append(str(self.users[key]))
        return "\n".join(s)

    def put(self, screenname, nFriends):
        screennameLower = screenname.lower()
        user = self.users.get(screennameLower)
        if user:
            user.nFriends = nFriends
            user.setState("queued")
        else:
            user = User(screenname, nFriends)
            self.users[screennameLower] = user
        log.msg("Adding screenname %r to request queue." % screenname)
        self.clean = False
        self.rdq.put(user)

    def _addUser(self, user):
        def _added(result):
            user.setState("added")
            self.clean = False
            return result

        def _failed(fail):
            self.clean = False
            if fail.check(ftwitter.Canceled):
                # The state has been changed to canceled below.
                assert user.canceled()
                log.msg("Addition of user %r canceled." % user.screenname)
            else:
                user.setState("failed")
                log.msg("Failed to add %r: %s" % (user.screenname, fail))

        log.msg("User %r received from request queue." % user.screenname)
        user.setState("underway")
        d = ftwitter.addUserByScreenname(self.cache, self.endpoint, user)
        d.addCallbacks(_added, _failed)
        d.addErrback(log.err)
        return d

    def cancel(self, screenname):
        log.msg("Attempting cancel of %r addition." % screenname)
        try:
            user = self.users[screenname.lower()]
        except KeyError:
            raise Exception("Cannot cancel unknown user %r." % screenname)
        else:
            if user.state == "underway":
                for item in self.rdq.underway():
                    if item.job.screenname == screenname:
                        log.msg("Cancelling underway %r addition." % screenname)
                        item.cancel()
                        user.setState("canceled")
                        break
                else:
                    raise Exception("Could not find %r in underway list." % screenname)
            elif user.state == "queued":
                queued = self.rdq.pending()
                for i, u in enumerate(queued):
                    if u.screenname == screenname:
                        del queued[i]
                        user.setState("canceled")
                        log.msg("Canceled queued %r addition." % screenname)
                        break
#.........这里部分代码省略.........
开发者ID:jdunck,项目名称:Tickery,代码行数:103,代码来源:adder.py

示例12: AdderCache

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import put [as 别名]
class AdderCache(DumpingCache):

    def __init__(self, cache, queueWidth, endpoint):
        super(AdderCache, self).__init__()
        self.cache = cache
        self.queueWidth = queueWidth
        self.endpoint = endpoint

    def load(self, cacheFile):
        self.rdq = ResizableDispatchQueue(self._addUser, width=self.queueWidth)
        self.users = super(AdderCache, self).load(cacheFile)
        if self.users is None:
            self.users = {}
            self.setCache(self.users)
        else:
            added = [u for u in self.users.values() if u.state == 'added']
            notAdded = [u for u in self.users.values() if u.state != 'added']
            log.msg('Loaded adder cache: found %d added, %d unadded users' %
                    (len(added), len(notAdded)))
            if self.cache.restoreAddQueue:
                log.msg('Restoring add queue.')
                # Re-queue, in original queue order, any users that are
                # marked as having been added.
                for user in sorted(notAdded, key=attrgetter('queuedAt')):
                    log.msg('Restoring %r (previous state %r)' %
                            (user.screenname, user.state))
                    user.reset()
                    d = self.rdq.put(user)
                    d.addErrback(self._reportCancelled, user.screenname)
                    self.clean = False
            else:
                log.msg('Not restoring formerly queued names.')
                # Drop users that were not added last time.
                for user in notAdded:
                    log.msg('Dropping user %r (in state %r)' %
                            (user.screenname, user.state))
                    del self.users[user.screenname.lower()]
                    self.clean = False

    def __str__(self):
        s = ['%d users in adder cache' % len(self.users)]
        for key in sorted(self.users.keys()):
            s.append(str(self.users[key]))
        return '\n'.join(s)

    def put(self, screenname, nFriends):
        screennameLower = screenname.lower()
        user = self.users.get(screennameLower)
        if user:
            user.nFriends = nFriends
            user.setState('queued')
        else:
            user = User(screenname, nFriends)
            self.users[screennameLower] = user
        log.msg('Adding screenname %r to request queue.' % screenname)
        self.clean = False
        d = self.rdq.put(user)
        d.addErrback(self._reportCancelled, screenname)

    def _addUser(self, user):
        def _added(result):
            user.setState('added')
            self.clean = False
            return result

        def _failed(fail):
            self.clean = False
            if fail.check(ftwitter.Canceled):
                # The state has been changed to canceled below.
                assert user.canceled()
                log.msg('Addition of user %r canceled.' % user.screenname)
            else:
                user.setState('failed')
                log.msg('Failed to add %r: %s' % (user.screenname, fail))

        log.msg('User %r received from request queue.' % user.screenname)
        user.setState('underway')
        d = ftwitter.addUserByScreenname(self.cache, self.endpoint, user)
        d.addCallbacks(_added, _failed)
        d.addErrback(log.err)
        return d

    def cancel(self, screenname):
        log.msg('Attempting cancel of %r addition.' % screenname)
        try:
            user = self.users[screenname.lower()]
        except KeyError:
            raise Exception('Cannot cancel unknown user %r.' % screenname)
        else:
            if user.state == 'underway' or user.state == 'queued':
                for job in self.rdq.underway() + self.rdq.pending():
                    u = job.jobarg
                    if u.screenname == screenname:
                        log.msg('Cancelling %s %r addition.' %
                                (user.state, screenname))
                        job.cancel()
                        user.setState('canceled')
                        break
                else:
                    raise Exception('Could not find %r in underway '
#.........这里部分代码省略.........
开发者ID:jkakar,项目名称:Tickery,代码行数:103,代码来源:adder.py


注:本文中的txrdq.rdq.ResizableDispatchQueue.put方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。