collect data using threads

Toby Dickenson tdickenson at devmail.geminidataloggers.co.uk
Tue Jun 14 13:06:24 EDT 2005


On Tuesday 14 June 2005 17:47, Peter Hansen wrote:
> Kent Johnson wrote:
> > Peter Hansen wrote:
> >> That will not work, and you will get data loss, as Jeremy points out.
> >>
> > 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 = []
> 
> Ah, since the entire list is being returned, you appear to be correct. 
> Interesting... this means the OP's code is actually appending things to 
> a list, over and over (presumably), then returning a reference to that 
> list and rebinding the internal variable to a new list.  If another 
> thread calls on_received() and causes new data to be appended to "the 
> list" between those two statements, then it will show up in the returned 
> list (rather magically, at least to my way of looking at it) and will 
> not in fact be lost.

But it might not "show up" until too late.

The consumer thread that called get_data presumably does something with that 
list, such as iterating over its contents. It might only "show up" after that 
iteration has finished, when the consumer has discarded its reference to the 
shared list.

-- 
Toby Dickenson



More information about the Python-list mailing list