473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Newbie having issues with threads

I'm a newbie trying to write a script that uses threads. I'm right
now a little bit stuck in understanding why the code snippet I wrote
doesn't seem to be entering the function defined in the
start_new_threa d() call.

If I run it as is (the threaded version), the output is:

UA_1 configuring...
UA_1 halting..

But if I comment out the line w/ the thread and just call the function
directly, everything seems to work OK:

UA_1 configuring...
UA_1 executing...
UA_1 halting...

Can anyone tell me why the thread doesn't seem to invoke the function
"execute()" ? I'm running Python 2.4.3.

Here is my code:

===========
import thread

class Test(object):
def __init__(self, instanceID):
self.instanceID = instanceID
def configure(self) :
print self.instanceID + " configuring..."
def execute(self):
print self.instanceID + " executing..."
def halt(self):
print self.instanceID + " halting..."

if __name__ == "__main__":
"""usage: sipp_auto [options]"""

ua1 = Test("UA_1")

ua1.configure()

#ua1.execute()
thread.start_ne w_thread(ua1.ex ecute, ())

ua1.halt()

===========
Thanks, James
Jul 31 '08 #1
2 971
Well, that seemed to do the trick. Thanks for the tip! I guess as a
novice and having no investment in the older "thread" module, I'll
just use the Threading module from now on.

James
=====

PS Here is my new code snippet:
=====

#!/usr/bin/python

import threading

class Test(object):
def __init__(self, instanceID):
self.instanceID = instanceID
def configure(self) :
print self.instanceID + " configuring..."
def execute(self):
print self.instanceID + " executing..."
def halt(self):
print self.instanceID + " halting..."

class testThread(thre ading.Thread):
def __init__(self, ID):
self.ID = ID
threading.Threa d.__init__(self )
def run(self):
ua1.execute()

if __name__ == "__main__":
"""usage: sipp_auto [options]"""

ua1 = Test("UA_1")

ua1.configure()

thread = testThread("UA_ 1")
thread.start()
thread.join()

ua1.halt()
Aug 1 '08 #2
On Thu, 31 Jul 2008 14:09:12 -0700, James Calivar wrote:
I'm a newbie trying to write a script that uses threads. I'm right now
a little bit stuck in understanding why the code snippet I wrote doesn't
seem to be entering the function defined in the start_new_threa d() call.

If I run it as is (the threaded version), the output is:

UA_1 configuring...
UA_1 halting..

But if I comment out the line w/ the thread and just call the function
directly, everything seems to work OK:

UA_1 configuring...
UA_1 executing...
UA_1 halting...

Can anyone tell me why the thread doesn't seem to invoke the function
"execute()" ? I'm running Python 2.4.3.

Here is my code:

===========
import thread

class Test(object):
def __init__(self, instanceID):
self.instanceID = instanceID
def configure(self) :
print self.instanceID + " configuring..."
def execute(self):
print self.instanceID + " executing..."
def halt(self):
print self.instanceID + " halting..."

if __name__ == "__main__":
"""usage: sipp_auto [options]"""

ua1 = Test("UA_1")

ua1.configure()

#ua1.execute()
thread.start_ne w_thread(ua1.ex ecute, ())

ua1.halt()

===========
Thanks, James
I've run into this problem before. The main problem is that the thread
started via start_new_threa d exits when the program does(instead of
finishing its work first). So what happens here is that sometimes it may
have the time to execute, and sometimes it won't

changing:
thread.start_ne w_thread(ua1.ex ecute, ())

ua1.halt()

to:
thread.start_ne w_thread(ua1.ex ecute, ())
from time import sleep
sleep(0.01) # this is barely even noticeable

ua1.halt()

fixes the problem in this case and the output is correct.

However, you seem to have taken up using the threading module which is
probably better anyway, just wanted to explain why this was happening.
Aug 1 '08 #3

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

Similar topics

3
3537
by: Jason Kistler | last post by:
I am having some serious issues trying to set the "importance" of an ASP email. I am using the CDO.Message object. Here is the code: <% Dim recipients recipients = Request.Form("Jason") & Request.Form("Debbie") Dim Groups Groups = Request.Form("Sales") & Request.Form("FieldServices") & Request.Form("CSC") & Request.Form("SupplyChain")
2
1008
by: Chris | last post by:
ok, here is what I am trying to do. I have a frameset with 2 aspx pages. After someone clicks on a button in the control page, I want to pass a SQL Query in a form of the string to the results page In the control Page I have the following code on button click. Session = SQL;...
8
1907
by: Viken Karaguesian | last post by:
Hello all, I'll start with this question: Can I assign an ID *and* a CLASS to a DIV? I am under the impression that you can. I'm having a problem that I can't seem to figure out. Some background info: I'm an amateur who makes sites for fun. I'm creating a website for my son's small school. It's a table-less design. I'm basically using a...
3
2595
by: Chris | last post by:
Don't know if there is a simple solution for this one or not. When running SQL server on a machine with 2000 loaded and the complete SQL package I don't have any issues. Now I'm trying to login using my XP machine with MSSQL developer edition, and I can not connect to my remote servers via TCP/IP. It can see the server, and it establishes a...
2
6758
by: SAL | last post by:
Below is similar code that I am have and the line "myDataConnect mydata = new myDataConnect(ConfigurationSettings.AppSettings);" is what I am having trouble with. The syntax is correct and I get no errors, but it is not finding the Key “connectSQL” or any key for that matter in my app.config. It returns a NULL value. Besides the...
4
2137
by: Bucco | last post by:
I installed python 2.5b3 on my windows XP sp2 box without any issues. I can double click the python program, and idle comes up in the command line window. However when I run python from the command line program cmd.exe, I get a pop-up window with the following error: 16 bit Windows Subsystem ----------------------------------------- The...
17
2209
by: Chad | last post by:
I'm want static char *output; to hold the modified string "tel chad" However, when I debug it, static char *output holds the ascii value of the strng, and not the string itself. Here is what I have. I know gets(), strcat strcpy() shouldn't be used.
2
1505
by: Damas | last post by:
Hello, First of all please excuse my vocabulary, english isn't my mother tong. I'm having a hard time trying to do this. I have two tables in a MySQL db, first one is for members datas, second one if the one for french departments (geography). I need to have a list of every department followed by the list of members that are living in this...
3
2359
by: RPhlb | last post by:
This is my first post, so excuse me if I don't get this right the first time. I have an issue where when I use DropDownList I only get the first, or "SelectedValue" back when I update a GridView Edit. I have been trying to hunt down a viable soltion to this, seems that many people out there have similar issues and the ...SelectedItem.Value or...
0
7698
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, well explore What is ONU, What Is Router, ONU & Routers main...
0
7612
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
7924
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
7970
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
5513
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
5219
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
3653
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...
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.