Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Question about thread

Reply
Thread Tools

Question about thread

 
 
Valkyrie
Guest
Posts: n/a
 
      11-19-2004
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
 
Reply With Quote
 
 
 
 
Valkyrie
Guest
Posts: n/a
 
      11-19-2004
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.


Valkyrie wrote:
> 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

 
Reply With Quote
 
 
 
 
Russell Blau
Guest
Posts: n/a
 
      11-19-2004
"Valkyrie" <> wrote in message
news:1100875373.911763@eng-ser6...
> 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.
>>> 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,))
....
1960
836
232
2864
3692
>>> Received 0

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.


--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.


 
Reply With Quote
 
Valkyrie
Guest
Posts: n/a
 
      11-19-2004
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 Blau wrote:
> "Valkyrie" <> wrote in message
> news:1100875373.911763@eng-ser6...
>
>>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.
>
>>>>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,))
> ...
> 1960
> 836
> 232
> 2864
> 3692
>
>>>>Received 0

>
> 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.
>
>

 
Reply With Quote
 
Peter Hickman
Guest
Posts: n/a
 
      11-19-2004
Valkyrie wrote:
> 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.
 
Reply With Quote
 
Craig Ringer
Guest
Posts: n/a
 
      11-19-2004
On Fri, 2004-11-19 at 23:45, Valkyrie wrote:
> 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

--
Craig Ringer

 
Reply With Quote
 
Valkyrie
Guest
Posts: n/a
 
      11-19-2004
Thank you for your advice. I have quite seriously searched for it in fact, but
just without the luck on finding such article...

Craig Ringer wrote:
> On Fri, 2004-11-19 at 23:45, Valkyrie wrote:
>
>>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
>
> --
> Craig Ringer
>

 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      11-19-2004
Valkyrie wrote:
> 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

 
Reply With Quote
 
Jarek Zgoda
Guest
Posts: n/a
 
      11-19-2004
Craig Ringer wrote:

>>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


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.

--
Jarek Zgoda
http://jpa.berlios.de/ | http://www.zgodowie.org/
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-19-2004
>
> 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.

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
Thread was being aborted in win2003 server. Back ground thread reading MS access database, no redirects or transfers. Johanna ASP .Net 0 10-13-2004 01:32 PM
"Thread was being aborted" error from WebApp using Thread.Sleep. Stephen Miller ASP .Net 3 07-01-2004 11:50 PM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM
PyNew_Interpreter(): "Fatal Python error, invalid thread state for this thread" question vincent wehren Python 0 12-11-2003 08:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57