Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9116 > unrolled thread

Re: why the following python program does not face any concurrency problems without synchronize mechanism?

Started byAlexander Kapps <alex.kapps@web.de>
First post2011-07-09 23:17 +0200
Last post2011-07-09 23:17 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: why the following python program does not face any concurrency problems without synchronize mechanism? Alexander Kapps <alex.kapps@web.de> - 2011-07-09 23:17 +0200

#9116 — Re: why the following python program does not face any concurrency problems without synchronize mechanism?

FromAlexander Kapps <alex.kapps@web.de>
Date2011-07-09 23:17 +0200
SubjectRe: why the following python program does not face any concurrency problems without synchronize mechanism?
Message-ID<mailman.804.1310246258.1164.python-list@python.org>
On 09.07.2011 22:45, smith jack wrote:
> from threading import Thread
>
> def calc(start, end):
>      total = 0;
>      for i in range(start, end + 1):
>          total += i;
>      print '----------------------result:', total
>      return total
>
> t = Thread(target=calc, args=(1,100))
> t.start()
>
> I have run this program for many times,and the result is always 5050,
> if there is any concurrency problem, the result should not be 5050,
> which is never met, anyhow
> I mean this program should get the wrong answer at some times, but
> this never happens, why?
> can concurrency without synchronize mechanism always get the right answer?
> any special case in python programming?

Why do you think, that there's a concurrency problem?

All variables are local to the calc() function and all calc() 
invocations run in an own thread. No thread tries to access any 
shared data, so why should there be a concurrency problem?

Concurrency is an issue, when two or more threads/processes try to 
access the same data, but in your program everything is local to the 
calc() function.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web