FAQ
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?

Search Discussions

  • Alexander Kapps at Jul 9, 2011 at 9:17 pm

    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.
  • TheSaint at Jul 10, 2011 at 2:50 pm

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 9, '11 at 8:45p
activeJul 10, '11 at 2:50p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase