Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11338 > unrolled thread

thread and process

Started by"守株待兔" <1248283536@qq.com>
First post2011-08-13 17:09 +0800
Last post2011-08-13 03:52 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  thread and process "守株待兔" <1248283536@qq.com> - 2011-08-13 17:09 +0800
    Re: thread and process aspineux <aspineux@gmail.com> - 2011-08-13 03:01 -0700
    Re: thread and process Carl Banks <pavlovevidence@gmail.com> - 2011-08-13 03:52 -0700

#11338 — thread and process

From"守株待兔" <1248283536@qq.com>
Date2011-08-13 17:09 +0800
Subjectthread and process
Message-ID<mailman.2251.1313226603.1164.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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!!

[toc] | [next] | [standalone]


#11339

Fromaspineux <aspineux@gmail.com>
Date2011-08-13 03:01 -0700
Message-ID<b7acf382-cbbd-48bd-a64c-342a8a3b12d7@z7g2000vbp.googlegroups.com>
In reply to#11338
On Aug 13, 11:09 am, "守株待兔" <1248283...@qq.com> 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

a=-1216477504
os.fork()
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
Internet.
Something like don't fork a process that already has tread(),
or try to keep all your threads inside the same process ...

Regards




[toc] | [prev] | [next] | [standalone]


#11341

FromCarl Banks <pavlovevidence@gmail.com>
Date2011-08-13 03:52 -0700
Message-ID<b77c84a9-f245-420e-a567-4978c8203096@glegroupsg2000goo.googlegroups.com>
In reply to#11338
On Saturday, August 13, 2011 2:09:55 AM UTC-7, 守株待兔 wrote:
> please see my code:
> import os
> import  threading
> print  threading.currentThread()  
> print &quot;i am parent &quot;,os.getpid()
> ret  =  os.fork()
> print  &quot;i am here&quot;,os.getpid()
> print  threading.currentThread()
> if  ret  ==  0:
>          print  threading.currentThread()
> else:
>         os.wait()
>         print  threading.currentThread()
>         
>         
> print &quot;i am runing,who am i? &quot;,os.getpid(),threading.<WBR>currentThread()
> 
> the output is:
> &lt;_MainThread(MainThread, started -1216477504)&gt;
> i am parent  13495
> i am here 13495
> &lt;_MainThread(MainThread, started -1216477504)&gt;
> i am here 13496
> &lt;_MainThread(MainThread, started -1216477504)&gt;
> &lt;_MainThread(MainThread, started -1216477504)&gt;
> i am runing,who am i?  13496 &lt;_MainThread(MainThread, started -1216477504)&gt;
> &lt;_MainThread(MainThread, started -1216477504)&gt;
> i am runing,who am i?  13495 &lt;_MainThread(MainThread, started -1216477504)&gt;
> it is so strange that  two  different  processes  use one  mainthread!!


They don't use one main thread; it's just that each process's main thread has the same name.  Which makes sense: when you fork a process all the data in the process has to remain valid in both parent and child, so any pointers would have to have the same value (and the -1216477504 happens to be the value of that pointer cast to an int).


Carl Banks

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web