collect data using threads

Qiangning Hong hongqn at gmail.com
Tue Jun 14 12:59:35 EDT 2005


James Tanis wrote:

> # > > > A class Collector, it spawns several threads to read from serial port.
> # > > > Collector.get_data() will get all the data they have read since last
> # > > > call.  Who can tell me whether my implementation correct?
> # > > >      
> # Here's the original code:
> # 
> # class Collector(object):
> #    def __init__(self):
> #        self.data = []
> #        spawn_work_bees(callback=self.on_received)
> # 
> #    def on_received(self, a_piece_of_data):
> #        """This callback is executed in work bee threads!"""
> #        self.data.append(a_piece_of_data)
> # 
> #    def get_data(self):
> #        x = self.data
> #        self.data = []
> #        return x
> # 
> I may be wrong here, but shouldn't you just use a stack, or in other 
> words, use the list as a stack and just pop the data off the top. I 
> believe there is a method pop() already supplied for you. Since 
> you wouldn't require an self.data = [] this should allow you to safely 
> remove the data you've already seen without accidentally removing data 
> that may have been added in the mean time.
> 

I am the original poster.

I actually had considered Queue and pop() before I wrote the above code.
 However, because there is a lot of data to get every time I call
get_data(), I want a more CPU friendly way to avoid the while-loop and
empty checking, and then the above code comes out.  But I am not very
sure whether it will cause serious problem or not, so I ask here.  If
anyone can prove it is correct, I'll use it in my program, else I'll go
back to the Queue solution.

To Jeremy Jones: I am very sorry to take you too much effort on this
weird code.  I should make it clear that there is only *one* thread (the
main thread in my application) calls the get_data() method,
periodically, driven by a timer.  And for on_received(), there may be up
to 16 threads accessing it simultaneously.


-- 
Qiangning Hong

 ___________________________________________________________
/ BOFH Excuse #208:                                         \
|                                                           |
| Your mail is being routed through Germany ... and they're |
\ censoring us.                                             /
 -----------------------------------------------------------
  \     .    _  .
   \    |\_|/__/|
       / / \/ \  \
      /__|O||O|__ \
     |/_ \_/\_/ _\ |
     | | (____) | ||
     \/\___/\__/  //
     (_/         ||
      |          ||
      |          ||\
       \        //_/
        \______//
       __ || __||
      (____(____)



More information about the Python-list mailing list