Question about thread

V

Valkyrie

Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?

def thrd(param): # the thread worker function
print "Received",param

import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))

Thanks in advance
 
V

Valkyrie

To be more precise, what I want to do is to have a threaded program to handle
some jobs concurrently (while those jobs are fetching information from the
Internet, so threading could speed up the process if the network is slow)

start
| (blahblahblah...)
v
+-----+-----+-----+-----+
| | | | |
--+-- --+-- --+-- --+-- --+--
| | | | | | | | | |
| A | | B | | C | | D | | E |
| | | | | | | | | |
--+-- --+-- --+-- --+-- --+--
| | | | |
+-----+-----+-----+-----+
| (blahblahblah...)
v
finish!

While process A-E will access the shared variables within the same program, and
each of those processes will have to parse a document on its own.
 
R

Russell Blau

Valkyrie said:
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?

def thrd(param): # the thread worker function
print "Received",param

import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))

You may ask, but when I tried your code, here is what happened:

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information..... print "Received",param
........ thread.start_new_thread(thrd,(i,))
....
1960
836
232
2864
3692Received 1
Received 2
Received 3
Received 4

Note that the numbers 1960, etc., appear to be the return values of the
thread.start_new_thread() function.
 
V

Valkyrie

When I do it line by line in python's console, I have similar result to youl.
But when I try to run it in a file, say:

python demo.py

It just returns me nothing. I have no idea on this right now...

Russell said:
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?

def thrd(param): # the thread worker function
print "Received",param

import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))


You may ask, but when I tried your code, here is what happened:

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.

... print "Received",param
...

... thread.start_new_thread(thrd,(i,))
...
1960
836
232
2864
3692

Received 1
Received 2
Received 3
Received 4

Note that the numbers 1960, etc., appear to be the return values of the
thread.start_new_thread() function.
 
P

Peter Hickman

Valkyrie said:
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?

def thrd(param): # the thread worker function
print "Received",param

import thread
for i in range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))

Thanks in advance

The problem with your script is that it will run too fast before it quits and
kills all the threads.

if you add

a = 0
for i in range(100000):
a = a + 1

to the end you will see the output.
 
C

Craig Ringer

When I do it line by line in python's console, I have similar result to youl.
But when I try to run it in a file, say:

python demo.py

It just returns me nothing. I have no idea on this right now...

You need to cause the main thread to wait on the child threads until
they've all finished, otherwise - as Peter Hickman just mentioned - your
script will terminate and all threads will end. Ten seconds on Google
found this:

http://www.faqts.com/knowledge_base/view.phtml/aid/1364
 
V

Valkyrie

Thank you for your advice. I have quite seriously searched for it in fact, but
just without the luck on finding such article...
 
S

Scott David Daniels

Valkyrie said:
Thank you for your advice. I have quite seriously searched for it in fact, but
just without the luck on finding such article...
Can you suggest where this information might have shown up
that you would have found it? If so, you may want to submit
a documentation bug or patch request. That is, repay the
group by making it more likely that the next person with your
problem finds their answer on their own.

--Scott David Daniels
(e-mail address removed)
 
J

Jarek Zgoda

Craig said:
You need to cause the main thread to wait on the child threads until
they've all finished, otherwise - as Peter Hickman just mentioned - your
script will terminate and all threads will end. Ten seconds on Google
found this:

http://www.faqts.com/knowledge_base/view.phtml/aid/1364

Are you sure? I keep observing that child threads continue its execution
until completion even if I do nothing in main thread. Consider this example:

import random, time
from threading import Thread

class Worker(Thread):

def __init__(self, name):
Thread.__init__(self)
self.name = name
print 'thread %s created' % self.name

def run(self):
amt = random.randint(1, 4)
print 'thread %s waiting %d seconds' % (self.name, amt)
time.sleep(amt)
print 'thread %s finished' % self.name

def __del__(self):
print 'thread %s is being destroyed' % self.name

if __name__ == '__main__':
for i in range(10):
t = Worker('%.04d' % (i + 1, ))
t.start()
print 'finished creating threads'

Even after printing "finished creating threads" all spawned threads
continue its execution up to final "thread nnnn is being destroyed", so
I think there's no need to take any special action (unless it's Python
on iSeries, buy this is another story) to wait for completion.
 
D

Diez B. Roggisch

Even after printing "finished creating threads" all spawned threads
continue its execution up to final "thread nnnn is being destroyed", so
I think there's no need to take any special action (unless it's Python
on iSeries, buy this is another story) to wait for completion.

The OP used the module "thread", while you used "threading" and didn't set
setDaemon(False). Then python waits until all threads are finished.

From the thread module docs:


When the main thread exits, it is system defined whether the other threads
survive. On SGI IRIX using the native thread implementation, they survive.
On most other systems, they are killed without executing try ... finally
clauses or executing object destructors.
 
C

Craig Ringer

Are you sure? I keep observing that child threads continue its execution
until completion even if I do nothing in main thread. Consider this example:

Well, I'm very far from an expert, but I've also experienced the
poster's reported behaviour even with quite simple programs. I'm
surprised to hear that the initially described behaviour isn't always
the case, and would be interested in knowing more about when it is, and
is not, generally encountered.
 
J

Jarek Zgoda

Diez said:
The OP used the module "thread", while you used "threading" and didn't set
setDaemon(False). Then python waits until all threads are finished.

Well... I was bit too fast in replying. ;)
From the thread module docs:

When the main thread exits, it is system defined whether the other threads
survive. On SGI IRIX using the native thread implementation, they survive.
On most other systems, they are killed without executing try ... finally
clauses or executing object destructors.

So it looks that using threading should be preferred over using thread,
as presented example works identically on Windows and on linux (it
doesn't work on AS/400, though...).
 
P

Peter Otten

Jarek said:
Are you sure? I keep observing that child threads continue its execution
until completion even if I do nothing in main thread. Consider this
example:

You are using the real thing - the highlevel threading.Thread class - not
the underlying thread.start_new_thread() function.
The threading module creates a _MainThread (a Thread subclass) instance that
registers itself as an atexit handler and waits for the other (non-demon)
Thread instances to terminate.

Failure-to-provide-the-source-code-will-ultimately-work-against
-companies-sitting-on-a-pile-of-crap-patents-ly yours

Peter
 
V

Valkyrie

I just looked up for the keywords "python thread example" in google and few
useful links are retrieved. It seems that most of the examples are too complicated.
 
V

Valkyrie

So may I ask is threading do exactly the same in terms of functionalities as
those in thread?
 
D

Diez B. Roggisch

Valkyrie said:
So may I ask is threading do exactly the same in terms of functionalities
as those in thread?

Yes, even more than thread. threading is considered the somewhat "better"
interface, as it exposes its funtionality in an objectorientied fashion -
and in this case, that's actually better :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top