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


Groups > comp.lang.python > #9179

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!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'instance': 0.05; 'typing': 0.05; 'prints': 0.07; '%s"': 0.09; 'caller': 0.09; 'line:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'skip:[ 30': 0.09; 'subject:problems': 0.09; 'thrown': 0.09; 'subject:python': 0.12; 'defined': 0.14; '"import': 0.16; 'bieber': 0.16; 'declaimed': 0.16; 'duplicating': 0.16; 'email addr:ix.netcom.com': 0.16; 'email name:wlfraed': 0.16; 'from:addr:ix.netcom.com': 0.16; 'from:addr:wlfraed': 0.16; 'from:name:dennis lee bieber': 0.16; 'function;': 0.16; 'initializes': 0.16; 'it).': 0.16; 'name)': 0.16; 'received:66.245': 0.16; 'received:dsl.mindspring.com': 0.16; 'received:mindspring.com': 0.16; 'received:wlfraed': 0.16; 'running:': 0.16; 't.start()': 0.16; 'threading': 0.16; 'url:netcom': 0.16; 'url:wlfraed': 0.16; 'wulfraed': 0.16; '>>>': 0.16; 'def': 0.16; '(which': 0.20; 'appears': 0.21; 'variable': 0.21; 'subject:not': 0.21; 'loop': 0.22; 'discussion': 0.22; 'end,': 0.23; 'code': 0.24; 'url:home': 0.25; 'statement': 0.25; '(and': 0.27; 'character': 0.28; 'lee': 0.28; 'bit': 0.28; 'import': 0.29; "didn't": 0.29; 'exit': 0.29; "skip:' 30": 0.29; 'code,': 0.29; 'example': 0.30; 'sun,': 0.30; 'value)': 0.30; 'compare': 0.31; 'subject:?': 0.31; 'values': 0.31; 'print': 0.32; 'to:addr:python-list': 0.34; 'header:X-Complaints-To:1': 0.34; 'calling': 0.34; 'there': 0.34; 'begins': 0.35; 'responded': 0.35; 'actual': 0.35; 'define': 0.35; 'charset:us-ascii': 0.36; 'skip:" 10': 0.36; 'thread': 0.37; 'received:org': 0.38; 'takes': 0.38; 'subject:: ': 0.38; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'total': 0.61; 'double': 0.62; 'contrary': 0.67; 'subject:program': 0.67; 'receive': 0.68; 'dennis': 0.77; 'subject:any': 0.84; 'subject:face': 0.84; 'total)': 0.84; 'sum': 0.89
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: why the following python program does not face any concurrency problems without synchronize mechanism?
Date Sun, 10 Jul 2011 13:55:01 -0700
Organization > Bestiaria Support Staff <
References <CAN1FwxfAZzeMy42KDN48W88rZ4JbZvyN0zzxFjsYeOP+kPKepw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host user-11fb7u1.dsl.mindspring.com
X-Newsreader Forte Agent 3.3/32.846
X-No-Archive YES
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.847.1310331314.1164.python-list@python.org> (permalink)
Lines 133
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1310331314 news.xs4all.nl 21805 [2001:888:2000:d::a6]:36344
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:9179

Show key headers only | View raw


	Duplicating from a prior post, since the OP appears to have
responded via private email (and I didn't double check to see if it was
a CC email before deleting it).

On Sun, 10 Jul 2011 04:45:30 +0800, smith jack <thinke365@gmail.com>
declaimed the following in gmane.comp.python.general:

> 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()
> 
	Contrary to said email, there are NO GLOBAL names defined in this
code snippet, and there is ONLY ONE THREAD RUNNING.

	If this is not the complete actual code, then all discussion is
moot.

	Line by line:

> from threading import Thread
	A bit of a waste in character count as "import Thread" takes more
typing than a single use of "threading.Thread" later.

> def calc(start, end):
	Begins a new scope for names

>	total = 0
	initializes a LOCAL name

>	for i in range(start, end + 1):
>		total += i;
	rebinds the LOCAL name to the sum of its prior contents and the
contents of "i" (which is also a local name)

>	print '----------------------result:', total
	prints the final value of the local variable after the loop ends

>	return total
	exit the thread function; the return value is thrown away (threads
do not return values to the caller as there is no calling statement to
receive the value)

> 
> t = Thread(target=calc, args=(1,100))
> t.start()
	define and start ONE INSTANCE of "calc" as a thread


	Compare an example with more than one thread running:

>>> import threading
>>> 
>>> def calc(start, end, who):
... 	total = 0
... 	for i in xrange(start, end+1):
... 		total += i
... 	print "\n%s computed: %s" % (who, total)
... 	return
... 
>>> thrds = [threading.Thread(target=calc, args=(1, 100, j)) for j in range(10)]
>>> for t in thrds:
... 	t.start()
... 

0 computed: 5050
 
2 computed: 5050
1 computed: 5050
 
 
3 computed: 5050
5 computed: 5050
4 computed: 5050
 

 
6 computed: 5050
8 computed: 5050
9 computed: 5050
7 computed: 5050



>>>  

	Compare that to one where "total" IS a global shared...

>>> def calc(start, end, who):
... 	global total
... 	total = 0
... 	for i in xrange(start, end+1):
... 		total += i
... 	print "\n%s computed: %s" % (who, total)
... 	return
... 
>>> thrds = [threading.Thread(target=calc, args=(1, 100, j)) for j in range(10)]
>>> for t in thrds:
... 	t.start()
... 

0 computed: 5050
 
2 computed: 3608
1 computed: 5825
 

3 computed: 7227
4 computed: 1915
 

5 computed: 3433
6 computed: 3700
 

8 computed: 1585
7 computed: 6000
9 computed: 6359


>>> 
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-07-10 13:55 -0700

csiph-web