thread and function

Cliff Wells logiplexsoftware at earthlink.net
Wed Jan 23 18:37:40 EST 2002


On Wed, 23 Jan 2002 23:16:54 -0000
maximilianscherr wrote:

> i use the thread module for this one thread.
> now i have this:
> 
> def test():
>    print "hi"
>    sleep(1)
>    print "hi2"
> 
> and call that this way:
> 
> start_thread#or something else, can't remember#(test, ())
>

Why don't you use the threading module rather than the thread module?  It
provides a higher-level interface to threading.  Redoing your example
above:

import threading

def test():
    print "hi"
    sleep(1)
    print "hi2"

t = threading.Thread(None, test)
t.start()

BTW, this example is next to worthless since it's more or less the same as
calling the function outside of a thread.  This example is only slightly
more interesting:

import threading
import time

def test(value):
    for i in range(10):
        print value, i
        time.sleep(1)

ta = threading.Thread(None, test, None, ('A',))
tb = threading.Thread(None, test, None, ('B',))
ta.start()
tb.start()


> in the description of that command, im told it exits if the function 
> returns, does this mean i have to put a return under my function?
> can i just type return or do i need to return a value?

You don't need an explicit return nor do you need to return anything.  When
the function finishes, it returns, that's what is meant.


-- 
Cliff Wells
Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308
(800) 735-0555 x308




More information about the Python-list mailing list