Is it a job for Thread or Process?

Cliff Wells logiplexsoftware at earthlink.net
Thu Nov 15 17:01:21 EST 2001


On Thursday 15 November 2001 13:48, A wrote:
> Hello,
> In my Python script I would like to start one function of this script
> in a different thread or process?
> That function sends an email.
> Can you please give me an example how I can start a new thread
> or process under Win32 systems?
>
>  Is it better to  use a new thread  or a new process for this task?
> Thank you for help.
> Ladislav

I usually use threads, as they are very simple under Python:

import threading
import time

def mythread(killevent):
    while not killevent.isSet():
        print "thread running"
        time.sleep(2)

killevent = threading.Event()
thread = threading.Thread(None, mythread, "", (killevent,))
thread.start() # start the thread

time.sleep(6) # let the thread run a little while
killevent.set() # tell it to stop
thread.join() # wait for it to actually exit

This demonstrates the basics for starting and stopping a thread (Python has 
no direct method for stopping a thread, hence the event).

Good luck,

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