why the following python program does not face any concurrencyproblems without synchronize mechanism

Discussion in 'Python' started by smith jack, Jul 9, 2011.

  1. smith jack

    smith jack Guest

    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?
    smith jack, Jul 9, 2011
    #1
    1. Advertising

  2. smith jack

    TheSaint Guest

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

    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
    TheSaint, Jul 10, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Muhammad Shuaib Anjum
    Replies:
    2
    Views:
    627
    Muhammad Shuaib Anjum
    Aug 13, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,744
    Smokey Grindel
    Dec 2, 2006
  3. Sh4wn
    Replies:
    163
    Views:
    2,565
    Luis Zarrabeitia
    Jun 12, 2008
  4. Muhammad Shuaib Anjum

    Does .NET provide any mechanism to replace ActiveX controls on web???

    Muhammad Shuaib Anjum, Aug 12, 2004, in forum: ASP .Net Building Controls
    Replies:
    7
    Views:
    438
    Jim Cheshire [MSFT]
    Aug 24, 2004
  5. Muhammad Shuaib Anjum

    Does .NET provide any mechanism to replace ActiveX controls on web???

    Muhammad Shuaib Anjum, Aug 12, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    94
    Muhammad Shuaib Anjum
    Aug 12, 2004
Loading...

Share This Page