Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #9115 > unrolled thread
| Started by | smith jack <thinke365@gmail.com> |
|---|---|
| First post | 2011-07-10 04:45 +0800 |
| Last post | 2011-07-10 22:50 +0800 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
why the following python program does not face any concurrency problems without synchronize mechanism? smith jack <thinke365@gmail.com> - 2011-07-10 04:45 +0800
Re: why the following python program does not face any concurrency problems without synchronize mechanism? TheSaint <nobody@nowhere.net.no> - 2011-07-10 22:50 +0800
| From | smith jack <thinke365@gmail.com> |
|---|---|
| Date | 2011-07-10 04:45 +0800 |
| Subject | why the following python program does not face any concurrency problems without synchronize mechanism? |
| Message-ID | <mailman.803.1310244333.1164.python-list@python.org> |
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?
[toc] | [next] | [standalone]
| From | TheSaint <nobody@nowhere.net.no> |
|---|---|
| Date | 2011-07-10 22:50 +0800 |
| Message-ID | <ivce8k$ccc$1@speranza.aioe.org> |
| In reply to | #9115 |
smith jack wrote: > have run this program for many times,and the result is always 5050 You might not need to make it in a multiprocess environment Try it in the python (3) shell >>> tot= 0 >>> for k in range(1,100): ... tot += k ... print(tot) ... And watch the risults. -- goto /dev/null
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web