collect data using threads

Qiangning Hong hongqn at gmail.com
Tue Jun 14 05:40:19 EDT 2005


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?

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 am not very sure about the get_data() method.  Will it cause data lose
if there is a thread is appending data to self.data at the same time?

Is there a more pythonic/standard recipe to collect thread data?

-- 
Qiangning Hong

 _______________________________________________
< Those who can, do; those who can't, simulate. >
 -----------------------------------------------
    \                                  ___-------___
     \                             _-~~             ~~-_
      \                         _-~                    /~-_
             /^\__/^\         /~  \                   /    \
           /|  O|| O|        /      \_______________/        \
          | |___||__|      /       /                \          \
          |          \    /      /                    \          \
          |   (_______) /______/                        \_________ \
          |         / /         \                      /            \
           \         \^\\         \                  /               \     /
             \         ||           \______________/      _-_       //\__//
               \       ||------_-~~-_ ------------- \ --/~   ~\    || __/
                 ~-----||====/~     |==================|       |/~~~~~
                  (_(__/  ./     /                    \_\      \.
                         (_(___/                         \_____)_)



More information about the Python-list mailing list