本文整理汇总了Python中txrdq.rdq.ResizableDispatchQueue.pending方法的典型用法代码示例。如果您正苦于以下问题:Python ResizableDispatchQueue.pending方法的具体用法?Python ResizableDispatchQueue.pending怎么用?Python ResizableDispatchQueue.pending使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类txrdq.rdq.ResizableDispatchQueue
的用法示例。
在下文中一共展示了ResizableDispatchQueue.pending方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testPause
# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import pending [as 别名]
def testPause(self):
items = range(3)
dq = ResizableDispatchQueue(self.slow, 1)
map(dq.put, items)
reactor.callLater(0.2, self._testPaused, dq)
yield task.deferLater(reactor, 0.1, dq.pause)
pendingJobs = dq.pending()
self.assertEqual([p.jobarg for p in pendingJobs], [1, 2])
示例2: testReprioritizePending
# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import pending [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)
示例3: testUnstarted
# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import pending [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()
示例4: testCancelRemovesFromQueue
# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import pending [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, [])
示例5: AdderCache
# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import pending [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
#.........这里部分代码省略.........
示例6: AdderCache
# 需要导入模块: from txrdq.rdq import ResizableDispatchQueue [as 别名]
# 或者: from txrdq.rdq.ResizableDispatchQueue import pending [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 '
#.........这里部分代码省略.........