[Tutor] forking process with python

Jonathan Hayward http://JonathansCorner.com jonathan.hayward at pobox.com
Sun Aug 17 03:16:13 EDT 2003


Jimmy verma wrote:

> Hello,
>
> I am having a problem in understanding this example from the o'rellys 
> book for python programming.
>
> #!/usr/bin/python
>
> def counter(count):
>     for i in range(count):
>         time.sleep(10)
>         print '[%s]--> %s ' %(os.getpid(), i)
>
>
> for i in range(10):
>     pid = os.fork()
>     if pid != 0:
>         print 'Process %d spawned' %pid
>     else:
>         counter(10)
>         os._exit(0)
>
> print 'Main process exiting.'
>
>
>
> I am not getting how this os.fork is working as when the child process 
> will be created, when the pid will be zero.
>
> As far i can understand is that fork returns the pid of the parent and 
> 0 as the return value for the child process( am i right ?).
>
> Kindly suggest sthing regarding the working of this example.
> If we have some links on the internet from where i can get information 
> related to forking process with python, pls point me towards that also. 

You've almost got it. When a process forks, the parent remains and a 
child is created. The return value for the *parent* is the *child's* 
PID, and the return value for the *child* is 0.

In terms of reference material, Alex Martelli and David Ascher's _Python 
Cookbook_ is useful. Pp. 229-231 discuss this, although it seems to me 
that you've got the basic concept; you just had one detail wrong.

>
> Thanks to all.
>
>
> Regards,
>
> Jim
>
> _________________________________________________________________
> MSN Hotmail now on your Mobile phone. 
> http://server1.msn.co.in/sp03/mobilesms/ Click here.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
++ Jonathan Hayward, jonathan.hayward at pobox.com
** To see an award-winning website with stories, essays, artwork,
** games, and a four-dimensional maze, why not visit my home page?
** All of this is waiting for you at http://JonathansCorner.com





More information about the Tutor mailing list