You are here: Home / Past Courses / Fall 2015 - ECPE 177 / Projects / Example Python3 Processes

Example Python3 Processes

Python Source icon processes.py — Python Source, 2 KB (2736 bytes)

File contents

#!/usr/bin/env python3

# Basic Python processes example
#
# Set script as executable via: chmod +x processes.py

import sys
import multiprocessing
import queue
import time

# Python-style process class
class ProcessDemo(multiprocessing.Process):
    def __init__(self, name, startNum, q):
        multiprocessing.Process.__init__(self)
        
        # Set any variables you want in your constructor
        self.name = name
        self.startNum = startNum
        self.q = q

    # The run() method of a MultiprocessWorker class is run when
    # process.start() is called. Do real work here
    def run(self):
        print("Running process '%s' starting at %d" % (self.name, self.startNum))
        i=self.startNum
        while(i < (self.startNum+10)):
            print(self.name + ", Count " + str(i))
            i=i+1

            # "Busy work" for demo program.
            # Otherwise, the processes will run so quickly
            # that they will finish before the scheduler
            # switches to a different process
            j=0
            while(j<400000):
                j=j+1

        # To exit the process, just return from the run() method
        try:
            while(True):
                q_value = self.q.get(block=False)
                print("Process '%s' has obtained value from Q: '%s'" % (self.name, q_value))
                time.sleep(0.01)   # Minimal sleep to let other process run
        except queue.Empty as msg:
            pass
            # This error is EXPECTED and NOT fatal!
            # We can't test if the queue is empty first, because
            # we might get a race condition with the other process

def main():
    print("Running in main()...")

    # Create a queue so that we can send info to the process
    # after it is running
    q = multiprocessing.Queue()

    print("Launching a pool of two processes...")
    process1 = ProcessDemo("Process 1", 100, q)
    process1.start()
    process2 = ProcessDemo("Process 2", 200, q)
    process2.start()
    print("Launched two processes...")

    # Build up a list of all processes
    # (to make it easy to wait on them)
    all_processes=[]
    all_processes.append(process1)
    all_processes.append(process2)
    
    # Add some data to the queue and see if the processes
    # will obtain it
    q.put("Q item 1")
    q.put("Q item 2")
    q.put("Q item 3")

    # Use the join() function to wait for a specific process to finish
    # (i.e. process1.join() or process2.join())
    print("Waiting for all processes to finish")
    for one_process in all_processes:
        one_process.join()
    print("All processes have finished")

    print("Exiting main()...")


if __name__ == "__main__":
    sys.exit(main())