# L-20.5 MCS 260 Fri 1 Aug 2014 : hello_multiprocess.py

"""
Illustration of the multiprocessing module in Python.
"""

from multiprocessing import Process
from time import sleep

def say_hello(name, tslp):
    """
    Process with name says hello,
    prints its process id, and
    sleeps for tslp seconds.
    """
    from os import getpid
    prt = 'hello from ' + name \
        + ' with process id ' + str(getpid())
    print prt
    print name, 'sleeps', tslp, 'seconds'
    sleep(tslp)
    print name, 'wakes up'

def main():
    """
    Defines two processes.
    """
    from os import getpid
    print 'the process id of main is', getpid()
    apr = Process(target=say_hello, args = ('A', 4))
    bpr = Process(target=say_hello, args = ('B', 1))
    print 'starting two processes...'
    apr.start()
    sleep(1) # to make printing look nice
    bpr.start()
    print 'waiting for processes to wake up...'
    apr.join()
    bpr.join()
    print 'processes are done'
    raw_input('hit enter to close window')

if __name__ == '__main__':
    main()
