Threads: Issue and suggestion required

Peter Hansen peter at engcorp.com
Tue Jul 19 03:33:28 CEST 2005


Sandeep Arya wrote:
> My query is. Is this possible that my main thread and my newly born 
> thread will execute their task simultaneously/parallel. (I read that 
> Python interpretor allows only one thread execution at a time. Using its 
> global lock. And switch among active thread based on time set by 
> setSwitchTime() function.. Is this correct?)

Where did you see something about a setSwitchTime() function?

Anyway, the statement about Python allowing "only one thread execution 
at a time" is wrong (allowing for the possibility of misunderstanding 
given that it's grammatically incorrect, so you might have meant 
something other than what I understand it as).  By definition, threads 
simulate "parallel units of execution", so certainly threads (in almost 
any system) appear to execute simultaneously.  In practice, of course, 
if you have only a single CPU then only one thing at a time can be 
happening (things like interrupts aside), and in an interpreter like 
Python's the same thing applies at a higher level: only one "bytecode" 
instruction at a time can be executing.  Nevertheless, threads in Python 
certainly "appear" to execute in parallel, whether you have two or ten, 
and the details are generally transparent at the level you are 
interested in...

> I just wannn know what is lifetime of thread. 

A threads runs until it ends...

> As I was planning to 
> implement one function such that:
> 
> 1.0 function starts
> 2.0 create socket
> 3.0 Open socket
> 4.0 Do whatever task to do
> 5.0 Close socket
> 6.0 Function ends
> 
> I wanna know that if i assign such above mentioned function to 
> thread::run then will the thread die after step 6.0 above i.e. function 
> ends.

Basically that is correct.  Anyway, threads in Python are trivial to 
play with, even at the interactive prompt, so why not just try them out 
and see how things go?

-Peter



More information about the Python-list mailing list