Ticket #4907 defect assigned
DeferredSemaphore __repr__ enhancement
| Reported by: | lewq | Owned by: | zintinio |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | core | Keywords: | easy |
| Cc: | thijs | Branch: | |
| Author: | Launchpad Bug: |
Description
It's easier to get a handle on DeferredSemaphore if you can print one and get meaningful statistics out of it:
def __str__(self):
return "<DeferredSemaphore with %i jobs max, %i jobs waiting, %i jobs running>" % (
self.limit, len(self.waiting), self.limit - self.tokens)
Adding the above enhancement, and adding this code to the documentation, will make it easier for a beginner to see what's going on:
sem = DeferredSemaphore(tokens=10)
def job(num):
# Simulate a long-running task
d = defer.Deferred()
reactor.callLater(1, d.callback, num)
return d
# Add 100 jobs to the queue
for i in range(100):
print sem
d = sem.run(job, i)
def job_done(data):
print "Job %i completed" % data
print sem
d.addCallback(job_done)
reactor.run()
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

