threading, how to?

Carl J. Van Arsdall cvanarsdall at mvista.com
Fri Apr 21 12:58:23 EDT 2006


akrapus wrote:
> Hi,
>
> I am trying to understand how to use threading in Python. I get
> threading as a concept, but not the implementation.
>
> In order to start threading, do you call it as a separate function,
> which will then be applied to the rest of the code (functions) or do
> you open threading in each function. This all can probably be answered
> by 'How python threads different functions'?
>
> Hope if somebody can drop me a few lines. I've been trying with
> different tutorials, but still do not understand.
>
> Cheers,
>
> Stevan
>   
So, I've played with python threading a fair bit, but if you don't need 
to delve too far into anything special you can get away with doing 
things fairly simply using the threading module

Basically (and this may be over simplifiied for this example), but you 
can create a thread() object and basically pass it a function pointer.  
Then you can control the thread object fairly easily with methods such 
as start() and join().

So, here's an example of how I might thread a function:

#BEGIN Code
def funcToThread():
  print "I'm a function"
 

myThread = threading.Thread(target=functToThread) #a function name 
without parens basically yields a function pointer
myThread.start() #Performs necessary tasks in order to run this thread

#Here might be other stuff in your main thread of execution

myThread.join() #say you want to force synchronization at some point

#END Code

Anyhow, there are more ways to do it and it really depends on your needs 
as to how far you need to take this. 

Hope that helps.

-carl





-- 

Carl J. Van Arsdall
cvanarsdall at mvista.com
Build and Release
MontaVista Software




More information about the Python-list mailing list