472,328 Members | 1,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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_thread() 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_new_thread(ua1.execute, ())

ua1.halt()

===========
Thanks, James
Jul 31 '08 #1
2 931
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(threading.Thread):
def __init__(self, ID):
self.ID = ID
threading.Thread.__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_thread() 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_new_thread(ua1.execute, ())

ua1.halt()

===========
Thanks, James
I've run into this problem before. The main problem is that the thread
started via start_new_thread 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_new_thread(ua1.execute, ())

ua1.halt()

to:
thread.start_new_thread(ua1.execute, ())
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
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...
2
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...
8
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...
3
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...
2
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...
4
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...
17
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...
2
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...
3
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.