classification
Title: multiprocessing Process examples: print and getppid
Type: Stage:
Components: Documentation Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: jnoller Nosy List: asksol, georg.brandl, jnoller, mnewman, sandro.tosi
Priority: normal Keywords:

Created on 2009-07-04 14:58 by mnewman, last changed 2010-10-13 05:16 by georg.brandl. This issue is now closed.

Messages (5)
msg90118 - (view) Author: Michael Newman (mnewman) Date: 2009-07-04 14:58
The "16.6.1.1. The Process class" section of the multiprocessing
documentation:
http://docs.python.org/dev/py3k/library/multiprocessing.html
has errors in both examples.

The first example needs the indentation fixed on the "from" and "if"
lines (remove the leading spaces).

The second example has two issues: print syntax needs be updated from
2.0 to 3.0 syntax. Also, getppid is not available on win32 platforms.
Below is a corrected example, which I tested successfully on on win32
and linux:

# Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win32
C:\>c:\Python31\python.exe Process_with_more_info.py
main line
module name: __main__
parent process: None
process id: 3216
function f
module name: __main__
parent process: 3216
process id: 3692
hello bob

# Python 3.0.1 (r301:69556, Jun  6 2009, 21:34:43) [GCC 4.3.2] on linux2
mike@www:~/files$ python3.0 Process_with_more_info.py
main line
module name: __main__
parent process: 19853
process id: 22544
function f
module name: __main__
parent process: 22544
process id: 22545
hello bob

# Start of corrected example:

from multiprocessing import Process, current_process
from sys import platform
import os

def info(title):
    print(title)
    print('module name:', __name__)
    if platform == 'win32':
      print('parent process:', current_process()._parent_pid)
    else:
      print('parent process:', os.getppid())
    print('process id:', os.getpid())

def f(name):
    info('function f')
    print('hello', name)

if __name__ == '__main__':
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

# End of corrected example.

I also saw this online:
"os.getppid on Windows"
http://d.hatena.ne.jp/chrono-meter/20090325/p1
But the license of the code is not clear, and it would make the example
too confusing to insert in.

Another reference:
"Multiprocessing docs are not 3.0-ready"
http://bugs.python.org/issue3256
Looks like some print statements fixed back then, but not sure why the
examples mentioned here were not fixed.
msg90121 - (view) Author: Michael Newman (mnewman) Date: 2009-07-04 15:48
# Revised example that is more platform neutral (avoids sys.platform):

from multiprocessing import Process, current_process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    if not hasattr(os, 'getppid'):  # win32
      print('parent process:', current_process()._parent_pid)
    else:
      print('parent process:', os.getppid())
    print('process id:', os.getpid())

def f(name):
    info('function f')
    print('hello', name)

if __name__ == '__main__':
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()
msg118134 - (view) Author: Sandro Tosi (sandro.tosi) * (Python committer) Date: 2010-10-07 21:11
Hello,
indentation problem has been fixed in r79901 and py3k syntax has been fixed in r74764.

wrt os.getppid(), in the documentation it's stated that with 3.2 it added support for windows too: I'd like someone running a windows box (I don't have a win system anywhere near to me) to check if the mp example works correctly as it is.

Regards,
Sandro
msg118497 - (view) Author: Michael Newman (mnewman) Date: 2010-10-13 01:12
The example is working correctly for:
Python 3.2a3 (r32a3:85355, Oct 10 2010, 17:11:45) [MSC v.1500 32 bit (Intel)] on win32

# First test:
C:\mike>c:\Python32\python.exe s2.py
main line
module name: __main__
parent process: 2360
process id: 1584
function f
module name: __main__
parent process: 1584
process id: 3588
hello bob

# Second test (run it again to see process IDs change)
C:\mike>c:\Python32\python.exe s2.py
main line
module name: __main__
parent process: 2360
process id: 5904
function f
module name: __main__
parent process: 5904
process id: 1560
hello bob

# For reference:
C:\mike>c:\Python32\python.exe
Python 3.2a3 (r32a3:85355, Oct 10 2010, 17:11:45) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getppid()
2360
msg118503 - (view) Author: Sandro Tosi (sandro.tosi) * (Python committer) Date: 2010-10-13 05:10
Thanks Michael for checking it, this bug can be closed then, since all the points mentioned were already fixed.
History
Date User Action Args
2010-10-13 05:16:03georg.brandlsetstatus: open -> closed
resolution: fixed
2010-10-13 05:10:56sandro.tosisetmessages: + msg118503
2010-10-13 01:12:01mnewmansetmessages: + msg118497
2010-10-07 21:11:26sandro.tosisetnosy: + sandro.tosi

messages: + msg118134
versions: + Python 3.2, - Python 3.1
2010-08-27 14:03:40asksolsetnosy: + asksol
2009-07-04 23:46:26jnollersetpriority: normal
2009-07-04 15:48:45mnewmansetmessages: + msg90121
2009-07-04 15:22:07benjamin.petersonsetassignee: georg.brandl -> jnoller

nosy: + jnoller
2009-07-04 14:58:43mnewmancreate