Results 1 to 10 of 10

Thread: Working with os module (python)

  1. #1
    Join Date
    Jan 2012
    Beans
    161

    Working with os module (python)

    I have this script :

    Code:
    while True:
    	parm += 1
    	pid = os.fork()
    	if pid == 0:
    		os.execlp('espeak', 'espeak','this is parm number'+str(parm))
    		assert False, 'error starting program'
    	else:
    		print('Child is',pid)
    		if input() == 'q' : break
    And it does say 'This is parm number (parm)' And it does print Child is (pid). Im confused as to how this works though. How is the pid of os.fork() == 0? Whenever I run os.fork() from the interpreter it is never 0......

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Working with os module (python)

    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Jan 2012
    Beans
    161

    Re: Working with os module (python)

    are there any conditions in which the child process id would not be 0?

  4. #4
    Join Date
    Jan 2012
    Beans
    161

    Re: Working with os module (python)

    another question. Why does

    os.fork, os.pid

    result in 2 tuples of 2 items each?

  5. #5
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Working with os module (python)

    Uh, and what is your problem, exactly? Based on what you say, the program seems to work as expected...
    「明後日の夕方には帰ってるからね。」


  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Working with os module (python)

    fork clones the code and the data, with the difference that in the original (parent) you get a non-zero value, while in the child you get 0. The difference allows you to recognize in which parallel 'universe' you are - child gets to run through the pid==0 branch and parent goes through else

    Code:
     parent            |  child
    -----------------------------------------------
    pid = os.fork()    | pid = os.fork()
    if pid == 0:       | if pid == 0:
        stuff          |     stuff
    else:              | else:
        stuff          |     stuff
    http://www.python-course.eu/forking.php
    Last edited by Vaphell; October 23rd, 2013 at 04:04 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Jan 2012
    Beans
    161

    Re: Working with os module (python)

    Vaphell I owe you a beer (or whatever). Thanks so much

  8. #8
    Join Date
    Jan 2012
    Beans
    161

    Re: Working with os module (python)

    So even though its miliseconds (or nano seconds) , it appears the parrent if/else block would execute first.....

  9. #9
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Working with os module (python)

    Quote Originally Posted by wingnut2626 View Post
    So even though its miliseconds (or nano seconds) , it appears the parrent if/else block would execute first.....
    What makes you think that? The if and else branches are executed in separate processes, and to all intents and purposes these are concurrent.

  10. #10
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Working with os module (python)

    Quote Originally Posted by spjackson View Post
    What makes you think that? The if and else branches are executed in separate processes, and to all intents and purposes these are concurrent.
    This point bears repeating (and emphasis). As it is, there is no way to predict which instruction (print or espeak) will be executed before the other. Assuming that one or the other will run first is a major mistake (called a race condition), which can have serious consequences. If for some reason you need to ensure that one or the other will run first, there are some mechanisms like mutexes which will let you achieve that, but unless you use such a mechanism, you must not assume either way.
    Last edited by Bachstelze; October 23rd, 2013 at 10:00 PM.
    「明後日の夕方には帰ってるからね。」


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •