[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: collect data using threads
From: Kent Johnson <kent37 () tds ! net>
Date: 2005-06-14 14:53:20
Message-ID: 42aeeef3$1_1 () newspeer2 ! tds ! net
[Download RAW message or body]
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
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic