Peter Hansen wrote: > 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? > > [snip sample with a list] > >> 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? > > > That will not work, and you will get data loss, as Jeremy points out. > > Normally Python lists are safe, but your key problem (in this code) is > that you are rebinding self.data to a new list! If another thread calls > on_received() just after the line "x = self.data" executes, then the new > data will never be seen.
Can you explain why not? self.data is still bound to the same list as x. At least if the execution sequence is x = self.data self.data.append(a_piece_of_data) self.data = [] ISTM it should work. I'm not arguing in favor of the original code, I'm just trying to understand your specific failure mode. Thanks, Kent -- http://mail.python.org/mailman/listinfo/python-list