use threading without classes

Peter Hansen peter at engcorp.com
Wed Aug 31 14:18:43 CEST 2005


billiejoex wrote:
> Hi all. Hi would like to use two threads in a program but all the examples I 
> found on the net use oop programming that I doesn't love too much. :-)
> Can you code me a short example in wich two different functions are executed 
> at the same time, plz?

import time
from threading import Thread

def func1():
     print 'first func running'
     time.sleep(1)
     print 'first func done'

def func2():
     print 'second func running'
     time.sleep(1)
     print 'second func done'

Thread(target=func1).start()
Thread(target=func2).start()

Note that this is still using "OOP programming", and you can't 
effectively avoid this in Python without jumping through more hoops than 
you're really interested in, but this basically avoids the need to do 
OOP things like subclassing.

-Peter



More information about the Python-list mailing list