# L-40 MCS 260 Wed 20 Apr 2016 : 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')

if __name__ == '__main__':
    main()
