collect data using threads
Jeremy Jones
zanesdad at bellsouth.net
Tue Jun 14 07:25:53 EDT 2005
Qiangning Hong 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?
>
>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?
>
>
>
This looks a little scary. If a thread is putting something in
self.data (in the on_received() method) when someone else is getting
something out (in the get_data() method), the data that is put into
self.data could conceivably be lost because you are pointing self.data
to an empty list each time get_data() is called and the list that
self.data was pointing to when on_received() was called may just be
dangling. Why not use the Queue from the Queue module? You can push
stuff in from one side and (have as many threads pushing stuff onto it
as you like) and pull stuff off from the other side (again, you can have
as many consumers as you'd like as well) in a thread safe manner.
HTH,
Jeremy Jones
More information about the Python-list
mailing list