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


Python ResizableDispatchQueue.underway方法代码示例

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


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

示例1: testUnstarted

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import underway [as 别名]
 def testUnstarted(self):
     """
     Check basic facts about a fresh instance of a
     ResizableDispatchQueue.
     """
     dq = ResizableDispatchQueue(None)
     self.assertEqual(dq.pending(), [])
     self.assertEqual(dq.underway(), set())
     self.assertFalse(dq.stopped)
     self.assertFalse(dq.paused)
     self.assertEqual((0, 0), dq.size())
     return dq.stop()
开发者ID:jcollie,项目名称:txrdq,代码行数:14,代码来源:test_rdq.py

示例2: AdderCache

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import underway [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

示例3: AdderCache

# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import underway [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.underway方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。