On Wed, 29 Mar 2023 at 16:56, Greg Ewing via Python-list
<python-list@python.org> wrote:
>
> On 28/03/23 2:25 pm, Travis Griggs wrote:
> > Interestingly the error also only started showing up when I switched from 
> > running a statistics.mean() on one of these, instead of what I had been 
> > using, a statistics.median(). Apparently the kind of iteration done in a 
> > mean, is more conflict prone than a median?
>
> It may be a matter of whether the GIL is held or not. I had a look
> at the source for deque, and it doesn't seem to explicitly do
> anything about locking, it just relies on the GIL.
>
> So maybe statistics.median() is implemented in C and statistics.mean()
> in Python, or something like that?
>

Both functions are implemented in Python, but median() starts out with
this notable line:

    data = sorted(data)

which gives back a copy, iterated over rapidly in C. All subsequent
work is done on that copy.

The same effect could be had with mean() by taking a snapshot using
list(q) and, I believe, would have the same effect (the source code
for the sorted() function begins by calling PySequence_List).

In any case, it makes *conceptual* sense to do your analysis on a copy
of the queue, thus ensuring that your stats are stable. The other
threads can keep going while you do your calculations, even if that
means changing the queue.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to