thread and process

Dave Angel davea at dejaviewphoto.com
Sat Aug 13 14:21:42 CEST 2011


On 01/-10/-28163 02:59 PM, 守株待兔 wrote:
> please see my code:
> import os
> import  threading
> print  threading.currentThread()
> print "i am parent ",os.getpid()
> ret  =  os.fork()
> print  "i am here",os.getpid()
> print  threading.currentThread()
> if  ret  ==  0:
>           print  threading.currentThread()
> else:
>          os.wait()
>          print  threading.currentThread()
>
>
> print "i am runing,who am i? ",os.getpid(),threading.currentThread()
>
> the output is:
> <_MainThread(MainThread, started -1216477504)>
> i am parent  13495
> i am here 13495
> <_MainThread(MainThread, started -1216477504)>
> i am here 13496
> <_MainThread(MainThread, started -1216477504)>
> <_MainThread(MainThread, started -1216477504)>
> i am runing,who am i?  13496<_MainThread(MainThread, started -1216477504)>
> <_MainThread(MainThread, started -1216477504)>
> i am runing,who am i?  13495<_MainThread(MainThread, started -1216477504)>
> it is so strange that  two  different  processes  use one  mainthread!!
Why would you figure that it's the same thread?  You're just looking at 
the ID of the thread object in each process, and ID's have no promise of 
being unique between different processes, nor between multiple runs of 
the same program.   In CPython, the id is actually an address, and each 
process has its own address space.  The addresses happen to be the same 
because the main thread was created before you forked.

DaveA




More information about the Python-list mailing list