Thread safety issue (I think) with defaultdict

Rustom Mody rustompmody at gmail.com
Thu Nov 2 23:19:59 EDT 2017


On Friday, November 3, 2017 at 6:28:28 AM UTC+5:30, Steve D'Aprano wrote:
> On Fri, 3 Nov 2017 07:24 am, Chris Angelico wrote:
> 
> > On Fri, Nov 3, 2017 at 3:27 AM, Israel Brewster wrote:
> >>
> >> Actually, that saying is about regular expressions, not threads :-) . In
> >> the end, threads are as good a way as handling concurrency as any other,
> >> and simpler than many. They have their drawbacks, of course, mainly in the
> >> area of overhead, and of course only multiprocessing can *really* take
> >> advantage of multiple cores/CPU's on a machine, but unlike regular
> >> expressions, threads aren't ugly or complicated. Only the details of
> >> dealing with concurrency make things complicated, and you'll have to deal
> >> with that in *any* concurrency model.
> >>
> > 
> > Thank you. I've had this argument with many people, smart people (like
> > Steven), people who haven't grokked that all concurrency has costs -
> 
> Of course I grok that all concurrency has costs. Apart from comparatively rare
> cases of "embarrassingly parallel" algorithms, any form of concurrent or
> parallel processing is significantly harder than sequential code.
> 
> 
> > that threads aren't magically more dangerous than other options.
> 
> There's nothing magical about it.
> 
> Threads are very much UNMAGICALLY more dangerous than other options because
> they combine:
> 
> - shared data; and
> 
> - non-deterministic task switching.

… which is to say «bad mix of imperative programming and concurrency»



«The world is concurrent» [Joe Armstrong creator of Erlang]

If you get up from your computer just now for a coffee, it does not mean I have
to at the same time. More pertinently, it would be rather wasteful if the
billion+ transistors of an i7 waited for each other rather than switching independently.

The problem is that von Neumann preferred to simplify the programming task along 
the lines nowadays called "imperative programming"… after whom we get the
terms "von Neumann model", "von Neumann machine" etc

IOW threads are a particularly extreme example of the deleterious effects
of stuffing the world into the mold of someone's (von Neumann's) brain.

ie shared data + task switching = combinatorially explosive results

Take your own statement «any form of concurrent or parallel processing is
significantly harder than sequential code» 

and apply it to the abc of imperative programming:

Problem: Interchange values of variables x and y

Layman answer:
x = y
y = x

[Ignore for a moment that python has an answer that is almost identical to the 
above and is correct: x,y = y,x]

"Correct" answer:
temp = x
x = y
y = temp

Correct? Really???
Or is that being trained to "think like a programmer" means learning to 
convolute our brains into an arbitrary and unnecessary sequentiality?



More information about the Python-list mailing list