[Tutor] Python performance resources & resouce usage hints
Hugo González Monteverde
hugonz-lists at h-lab.net
Fri Apr 7 18:42:37 CEST 2006
Liam Clarke wrote:
> Each thread's run() method basically looks like this -
>
> while True:
> try:
> data = self.queue.get(False)
> self.DAO.send_data(data)
> except Empty:
> if self.shutdown:
> print "\DAO closing"
> return
> continue
>
>
Hi Liam,
I checked the response by Kent. I completely agree with him in that the
problem is the endless polling you have.
However, you do not have to implement a sleep in your loop; the main
issue may be that you are not using the facilities of the 'get' method
in the queue. From the queue docs:
get( [block[, timeout]])
Remove and return an item from the queue. If optional args block is
true and timeout is None (the default), block if necessary until an item
is available. If timeout is a positive number, it blocks at most timeout
seconds and raises the Empty exception if no item was available within
that time. Otherwise (block is false), return an item if one is
immediately available, else raise the Empty exception (timeout is
ignored in that case).
You are not using the optional timeout and blocking which 'get' provides (!)
Try setting it and see your CPU usage go down. This will implement
blocking, and the queue will be used as soon as data is there. Set block
to True and a timeout if you need to use it (looks like you only need
blocking)
while True:
try:
data = self.queue.get(True)
self.DAO.send_data(data)
Hope that helps,
Hugo
More information about the Tutor
mailing list