473,499 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

beginner, thread & else

can someone explain me this code:
----------------------------------
import thread
stdoutmutex = thread.allocate_lock()
exitmutexes = [0] * 10

def counter(myId, count):
for i in range(count):
stdoutmutex.acquire()
print '[%s] =%s' % (myId, i)
stdoutmutex.release()
exitmutexes[myId] = 1 # signal main thread

for i in range(10):
thread.start_new(counter, (i, 100))

while 0 in exitmutexes:
pass
print 'Main thread exiting.'
-----------------------------------

thread.start_new(counter, (i, 100)) is running counter function.
Is counter function and while statement executed in same time (other
things i understand, but can't get into this)?

thanks
Dec 14 '06 #1
3 1200
Gigs_ wrote:
can someone explain me this code
did you write that yourself, or did you find it in some book or article?
----------------------------------
import thread
the thread module should not be used directly by application programs;
use the "threading" module instead.
stdoutmutex = thread.allocate_lock()
exitmutexes = [0] * 10

def counter(myId, count):
for i in range(count):
stdoutmutex.acquire()
print '[%s] =%s' % (myId, i)
stdoutmutex.release()
exitmutexes[myId] = 1 # signal main thread

for i in range(10):
thread.start_new(counter, (i, 100))

while 0 in exitmutexes:
pass
that's a "busy loop"; the CPU will spend all the cycles it can get
checking for the condition. that's not a good way to wait for
things.

use the "threading" module instead; by default, it waits for all threads
to finish without requiring flag lists and excessive CPU use.
print 'Main thread exiting.'
-----------------------------------

thread.start_new(counter, (i, 100)) is running counter function.
it starts a new instance of counter, in a separate thread.
Is counter function and while statement executed in same time (other
things i understand, but can't get into this)?
yes, the while loop will run in the "main" thread, in parallel with the
other threads. however, Python uses a global lock to synchronize access
to Python variables:

http://effbot.org/pyfaq/what-is-the-...reter-lock.htm

so the threads in a simple program like this won't run fully in parallel
on a multi-CPU machine.

</F>

Dec 15 '06 #2
"Fredrik Lundh" <fr*****@pythonware.comwrote:
Gigs_ wrote:
----------------------------------
import thread

the thread module should not be used directly by application programs;
use the "threading" module instead.
Ooops! - I am doing this, for long running stuff.

I was aware of threading, but I could not spot significant differences
in functionality, and thread seems to work, and so far I have not been
bitten (I think).

What are the dangers?

(Apart from the BogeyMan that might come and munch my code...)

- Hendrik

Dec 15 '06 #3
Fredrik Lundh wrote:
did you write that yourself, or did you find it in some book or article?
This is the example from programming python 2nd book, I use this just
for learning
Dec 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1558
by: Buck Turgidson | last post by:
I am trying an example from an O'Reilly book that I just can't get to work. Can someone tell me where I am going wrong. According to the book, I should see the values of the HTTP request echoed by...
13
18640
by: Martin | last post by:
I have 5 days of experince with XSLT and I am sure my problem is pretty much as basic as they come but I cannot work it out, so I hope someone will take pity on me - please! I have inherited an...
8
7573
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
7
9430
by: Brett Robichaud | last post by:
I'm trying to decide on the right approach for communication between the UI and a worker thread in a WinForms app. I am very familiar with threads in the unmanaged C++ world and in the past have...
5
2027
by: Robert W. | last post by:
I'm creating a WinForms app that will act as a companion (think administrator functionality) to a Pocket PC app. Generally the WinForms app works under just the UI thread. But if a Pocket PC...
14
5904
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
8
2233
by: jasson118 | last post by:
calculate the power of 2 integers. For example, power(2,3)=8, power(3,2)=9,power(4,0)=1. Not allow use power math function.Two integers are read from input by scanf. The program must produce an...
5
3507
by: JasonX | last post by:
Im having problems with my program, in that if I close the main form while my new thread is doing work which involves writing to the main form, then I get an error about a disposed object. To...
6
6760
by: rahulsengupta895 | last post by:
. #define MIN(a,b) (a<b?a:b) #define MAX(a,b) (a>b?a:b) #include "Video.h" #define NO_HUE -1
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7006
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7385
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.