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


Groups > comp.lang.python > #9116

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <alex.kapps@web.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'think,': 0.05; 'python': 0.08; 'answer?': 0.09; 'concurrency': 0.09; 'subject:problems': 0.09; 'subject:python': 0.12; 'wrote:': 0.15; 'invocations': 0.16; 'message-id:@web.de': 0.16; 'met,': 0.16; 'subject: \n ': 0.16; 't.start()': 0.16; 'thread.': 0.16; 'threading': 0.16; 'tries': 0.16; 'def': 0.16; 'issue,': 0.19; 'subject:not': 0.21; 'mechanism': 0.22; 'header:In-Reply-To:1': 0.22; 'problem?': 0.23; 'times,': 0.25; 'function': 0.26; 'import': 0.29; "skip:' 30": 0.29; 'variables': 0.29; 'from:addr:web.de': 0.30; 'subject:?': 0.31; 'shared': 0.32; 'print': 0.32; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'there': 0.34; 'function.': 0.35; 'data,': 0.35; 'thread': 0.37; 'some': 0.37; 'but': 0.37; 'subject:: ': 0.38; 'two': 0.38; 'run': 0.39; 'case': 0.39; 'should': 0.39; "there's": 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'total': 0.61; 'subject:program': 0.67; 'special': 0.67; 'received:172.20': 0.73; 'why?': 0.73; 'programming?': 0.84; 'sender:addr:web.de': 0.84; 'subject:any': 0.84; 'subject:face': 0.84
Date Sat, 09 Jul 2011 23:17:29 +0200
From Alexander Kapps <alex.kapps@web.de>
User-Agent Mozilla/5.0 (X11; U; Linux i686; de-DE; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10
MIME-Version 1.0
To python-list@python.org
Subject Re: why the following python program does not face any concurrency problems without synchronize mechanism?
References <CAN1FwxfAZzeMy42KDN48W88rZ4JbZvyN0zzxFjsYeOP+kPKepw@mail.gmail.com>
In-Reply-To <CAN1FwxfAZzeMy42KDN48W88rZ4JbZvyN0zzxFjsYeOP+kPKepw@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Sender alex.kapps@web.de
X-Sender Alex.Kapps@web.de
X-Provags-ID V01U2FsdGVkX18lX4bAdHCFyczeS48QZrH9QvZmCwQ9skNAcdHy 0ExRu8g5qpNJv+t4ufxl/AJuq0reWOql/nlP5QvuXLkYuLjdjC wZrBO9184=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.804.1310246258.1164.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1310246258 news.xs4all.nl 21883 [2001:888:2000:d::a6]:54099
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:9116

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web