thread and process

aspineux aspineux at
Sat Aug 13 12:01:07 CEST 2011

On Aug 13, 11:09 am, "守株待兔" <1248283... at> 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!!

You should not mix thread and fork.

Some hint :

You put your "import threading" before your fork(), then data
initialized by the import
are the same in the two process then it display the same, this is like

print a

second I thing -1216477504 has no meaning, this is not a system thread
ID but just an ID generated by python
I think they must be unique inside a process but not cross process.
Then 2 process can have the same python thread ID.

If you have to mix thread and fork try to find some hints from
Something like don't fork a process that already has tread(),
or try to keep all your threads inside the same process ...


More information about the Python-list mailing list