473,785 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

boolean flag vs threading.Event

I have a class similar to this:
class MyThread(thread ing.Thread):

def __init__(self):
self.terminated = False

def run(self):
while not self.terminated :
pass # do stuff here

def join(self):
self.terminated = True
threading.Threa d.join(self)
Recently I was reading in the Python Cookbook (9.2 Terminating a
Thread) about how to do this sort of thing. That recipe uses a
threading.Event object to signal the thread termination. Here's my
class recoded to use an event:
class MyThread(thread ing.Thread):

def __init__(self):
self.event = threading.Event ()

def run(self):
while not self.event.isSe t():
pass # do stuff here

def join(self):
self.event.set( )
threading.Threa d.join(self)
If I understand the GIL correctly, it synchronizes all access to
Python data structures (such as my boolean 'terminated' flag). If that
is the case, why bother using threading.Event for this purpose?

Thanks,
~ Daniel

Feb 27 '07 #1
2 5338
On 27 Feb 2007 13:37:12 -0800, Daniel <mi*******@gmai l.comwrote:
I have a class similar to this:
class MyThread(thread ing.Thread):

def __init__(self):
self.terminated = False

def run(self):
while not self.terminated :
pass # do stuff here

def join(self):
self.terminated = True
threading.Threa d.join(self)
Recently I was reading in the Python Cookbook (9.2 Terminating a
Thread) about how to do this sort of thing. That recipe uses a
threading.Event object to signal the thread termination. Here's my
class recoded to use an event:
class MyThread(thread ing.Thread):

def __init__(self):
self.event = threading.Event ()

def run(self):
while not self.event.isSe t():
pass # do stuff here

def join(self):
self.event.set( )
threading.Threa d.join(self)
If I understand the GIL correctly, it synchronizes all access to
Python data structures (such as my boolean 'terminated' flag). If that
is the case, why bother using threading.Event for this purpose?

The GIL is an implementation detail and relying on it to synchronize
things for you isn't futureproof. You're likely to have lots of
warning, but using threading.Event () isn't any harder, and it's more
correct and safer in the long term.

There's a whole bunch of other cases where you might want to use an
event, too. For example, you can have a single event which signals
multiple threads to stop, and you can wait on a thread without busy
looping.

If you don't care about doing any of those things, and you're
confident in relying on undocumented features of the GIL to protect
you, and you'll never port your code to a different Python
implementation, then I guess you can go ahead and use the boolean. But
what are you gaining, really?
Feb 27 '07 #2
But what are you gaining, really [by using a boolean flag instead of an Event]?

I agree Chris, the Event is better and it certainly does not add much
if any overhead. Thanks for the response.

~ Daniel

Feb 28 '07 #3

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

Similar topics

5
7746
by: asim | last post by:
Hi if a declare a variale say Dim Flag and assing it to true a boolean value Flag = True and if i try to concatinate it with another string like mystr="<my-str>" & Flag & "</my-str>" and if the machine is non english the mystr will contain something like <my-str>that language characters</my-str>
77
5390
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
16
17545
by: Ian Tuomi | last post by:
How can I define a boolean value in c? (an value that can only be either 1 or 0) I feel bad for the memory loss when declaring ints for variables that do not need that much memory. -- Ian Tuomi Jyväskylä, Finland "Very funny scotty, now beam down my clothes." GCS d- s+: a--- C++>$ L+>+++$ E- W+ N+ !o>+ w---
6
2883
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things, periodically calls a method in this TimerState object which raises an event to the startup form,...
4
2517
by: Patrick.O.Ige | last post by:
I'm DataBinding a CheckBoxList and i want to get the checkboxes selected when the page is loaded depending on a Boolean value from the Database.. chkDebtor.DataSource = objDR chkDebtor.DataValueField = "checked" -- this is a column in the table that returns 1 or 0 chkDebtor.DataTextField = "DebtorCode" chkDebtor.DataBind() Dim i As Integer = 0 While i < chkDebtor.Items.Count
4
5945
by: Joe HM | last post by:
Hello - I realize that there is no more IsMissing function in VB.NET but how can I have a boolean argument that is optional and in the code I need to determine whether it was passed it or not? I use the Single.NaN to set the default values for some of my optional single arguments so that I can determine whether an argument was passed it ... but that does not work for booleans.
76
4931
by: KimmoA | last post by:
First of all: I love C and think that it's beautiful. However, there is at least one MAJOR flaw: the lack of a boolean type. OK. Some of you might refer to C99 and its _Bool (what's up with the uppercase 'B' anyway?) and the header you can include (apparently) to get a real "bool". This isn't my point, however -- it should have been there from the beginning. char is a small int. We all know that. However, "char some_bool = 0;" simply...
4
19806
by: moondaddy | last post by:
I'm new to c# and want to know the preferred way to reverse a Boolean value. for example: in vb I can do this: dim flag as Boolean
11
15017
by: KiranJyothi | last post by:
Hello All, I am writing a code in C where I have to use the Boolean data type. I am doing - typedef int flag; #define FALSE 0 #define TRUE 1 void main()
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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 we have to send another system
2
3646
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.