473,583 Members | 2,878 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.acq uire()
print '[%s] =%s' % (myId, i)
stdoutmutex.rel ease()
exitmutexes[myId] = 1 # signal main thread

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

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

thread.start_ne w(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 1205
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.acq uire()
print '[%s] =%s' % (myId, i)
stdoutmutex.rel ease()
exitmutexes[myId] = 1 # signal main thread

for i in range(10):
thread.start_ne w(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_ne w(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*****@python ware.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
1566
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 the PHP. But obviously they're not there. All I see is "regionName is wineType is" Any help would be appreciated. // index.html <HTML>...
13
18650
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 XML file format which has one header node and several child nodes : <wi workitemid= "99999" .... etc .... /> <widata dataname="AMOUNT"...
8
7581
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 looping function the window becomes unmovable and cannot close under its own direction (the upper right "close 'X'") My first attempt to solve...
7
9439
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 used WM_USER based messages to communicate status from the worker thread back to the UI thread. What is the right way to do this in .Net? Are...
5
2035
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 connects to the desktop via ActiveSync then a separate thread is spawned. In addition to the main desktop app window I also have a Notification form...
14
5916
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 because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary...
8
2244
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 error message when the user enter a negative number, able to calculate the powers until terminate by user "by entering ctl-Z". output show both input...
5
3510
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 fix this, i have added an event, with the aim the main form subscribes to the event when it is created and unsubscribes as soon as its closed. This...
6
6771
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
7811
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8185
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5689
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3811
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.