473,382 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

wierd threading behavior

Hello,

I am just starting to play threading in python, here is a really
interesting problem I am very curious about:
"
import thread
def main():
thread.start_new(test.())

def test():
print 'hello'

main()
"
this program doesn't print out 'hello' as it is supposed to do.
while if I change main() into :
"
def main():
while 1:
thread.start_new(test.())
"
It goes on to print 'hello' forever.

while if I use:
"
def main():
for i in range(5):
print i
thread.start_new(test.())
"
It prints out 1,2,3,4,5 in main(), but still doesn't print out anything
from test()!

This is really wierd behavior for me, I am sure it's just something
simple&stupid, please enlighten me!

Thanks,
qun

Oct 14 '05 #1
6 1699
thread is a low-level threading module, and start_new is deprecated.
Aside from that, thread.start_new(test.()) is syntaxically wrong (a
pair of brackets can't follow a dot). However, your example does work
for me once I fix the syntax, and it prints hello but then hangs. I
can't explain the other results, though - possibly undefined behaviour
or more likely me not having much experience with the low-level thread
interface.

Use threading instead, like so:

import threading

def test():
print 'Test successful!'

def main():
thread = threading.Thread(target = test)
thread.start()

main()

Oct 14 '05 #2
Thanks Sam,
That was a stupid typo ( yeah, I actually typed it in :), it should be
(test,()).
I am using python 2.4.1 in ubuntu. I do aware that threading.Thread is
prefered,
but I did not realize thread is deprecated. It is still a mysterious
behavior anyhow. :)

Oct 14 '05 #3
Qun Cao:
import thread
def main():
thread.start_new(test.())

def test():
print 'hello'

main()
"
this program doesn't print out 'hello' as it is supposed to do.
while if I change main()


The program has exited before the thread has managed to run. It is
undefined behaviour whether secondary threads survive main thread
termination but it looks like they don't on your system.

Use the threading module and call join to wait for all threads to
complete before exiting the program.

Neil
Oct 15 '05 #4
Thanks Neil, that's very useful to know.

Qun

Oct 17 '05 #5
[Qun Cao]
import thread
def main():
thread.start_new(test.())

def test():
print 'hello'

main()
"
this program doesn't print out 'hello' as it is supposed to do.
while if I change main()

[Neil Hodgson] The program has exited before the thread has managed to run. It is
undefined behaviour whether secondary threads survive main thread
termination but it looks like they don't on your system.
In fact, they don't on most systems.
Use the threading module and call join to wait for all threads to
complete before exiting the program.


That's a different story: threads from the `thread` module have
entirely OS-specific behavior when Python shuts down. Python knows a
lot more about threads from the newer `threading` module, & uses an
atexit() hook to ensure that the Python interpreter does _not_ go away
while a threading.Thread is still running(*). IOW, Python does the
join() for you for threading.Thread threads -- there's no need to do
it yourself.

(*) Unless you explicitly mark it as a daemon thread.
Oct 18 '05 #6
> Hello,
I am just starting to play threading in python, here is a really
interesting problem I am very curious about:
"
import thread
def main():
thread.start_new(test.())


First, delete the dot after "test".
Second, is possibly that the Main() finishes before you can see the
print out of test().

Oct 18 '05 #7

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

Similar topics

19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
1
by: paul reed | last post by:
Hello, I am having some weird behavior between two machines...one which is running the 1.1 framework and one which is running 1.0. After opening a child form from a parent...I update the...
14
by: SStory | last post by:
I am trying to make a splash screen for my vb.net app. It is an mdi app. including the splash code produces wierd results. not inluding makes things fine. Also have tried loading the splash...
0
by: Tom | last post by:
OK, here's a wierd one... I have a listbox, which I fill with strings (in my case, file names). I normally load this via a loop, adding each item to the list box in the loop. I put lb.BeginUpdate...
1
by: Sačo Zagoranski | last post by:
Hi... This is driving me crazy and I really hope I can get an answer... Take a look at the piece of code below (I'm using .net 2.0): try { double pointTime =...
3
by: Tom | last post by:
We are experiencing some wierd debugging behavior. What happens is that, during debugging with VS 2003, the debugger seems to 'skip' statements that are associated with database operations. For...
4
by: Muthu Arumugam | last post by:
Tried the following c# code static void Main(string args) { ArrayList list = new ArrayList(); int i = 10;
10
by: =?Utf-8?B?ZHNoZW1lc2g=?= | last post by:
Hello, I have a C# project with windows forms. I sometimes get unprdictable .NET exceptions. I don't always get them, so I can't reproduce where exactly it happens. I can generally say it happens...
4
by: alex21 | last post by:
Ok im getting this exception from a DataGridView control in my program. System.ArgumentOutOfRangeException was unhandled Message="Value of '132' is not valid for 'Value'. 'Value' should be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.