From python-list Tue Jun 14 17:06:24 2005 From: Toby Dickenson Date: Tue, 14 Jun 2005 17:06:24 +0000 To: python-list Subject: Re: collect data using threads Message-Id: <200506141806.24706.tdickenson () devmail ! geminidataloggers ! co ! uk> X-MARC-Message: https://marc.info/?l=python-list&m=111876880016661 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 -- http://mail.python.org/mailman/listinfo/python-list