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?

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 2011-07-10 13:55 -0700
Organization > Bestiaria Support Staff <
References <CAN1FwxfAZzeMy42KDN48W88rZ4JbZvyN0zzxFjsYeOP+kPKepw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.847.1310331314.1164.python-list@python.org> (permalink)

Show all headers | 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