# ProcessSalesOrder.py # Chapter 6 Standard Modules # os module page 69 # sys module page 71 # subprocess module page 71 # Level 1 Course __main__ statement page 35 ''' Customer Sales Order -> Main Process 16 sec subprocesses + time Work Order -> shop work order interal process 4 sec Purchase Order -> vendor order get supplies 12 sec Process Starts subprocess 1 starts subprocess 2 starts wait for the subprocesses complete Process End ''' # ProcessSalesOrder.py import subprocess import sys import os import datetime # Sales Order Process def salesOrder(): print("Sales Order Process Starting...") # work order process workOrder_subprocess = subprocess.Popen( [ sys.executable, # calls an cmd.exe '-c', 'from ProcessSalesOrder import workOrder; workOrder()' ] ) # Popen() # purchase order process purchaseOrder_subprocess = subprocess.Popen( [ sys.executable, # calls an cmd.exe '-c', 'from ProcessSalesOrder import purchaseOrder; purchaseOrder()' ] ) # Popen() # At this point there is a main salesOrder process and 2 subprocesses running print("Sales Order is waiting for Work Order to Finish...") workOrder_subprocess.wait() print("Sales Order is waiting for Purchase Order to Finish...") purchaseOrder_subprocess.wait() # Now all the subprocesses have exited with Exit Code 0 or 1 (pass or fail) print("Now the subprocesses have finished, so the main process can continue...") # Finally print("Now the final cleanup happens...") # Work Order Process (subprocess) def workOrder(): print("Here is where the workOrder processes have run...") startTime = datetime.datetime.now() print(startTime) i = input("Process holding till enter key ...") # hold the subprocess and check task manager # Purchase Order Process (subprocess) def purchaseOrder(): print("Here is where the purchaseOrder processes have run...") startTime = datetime.datetime.now() print(startTime) i = input("Process holding till enter key ...") # hold the subprocess and check task manager # Main Bootstrap Function page 35 def main(): # Pre Process Operations # File I/O page 46 Level 1 dateStamp = datetime.datetime.now().strftime('%Y%m%d') # string # Log File Name to Output data # processingLog_20241213.txt log_filename = "processingLog_" + dateStamp + ".txt" # change sys.stdout() to file sys.stdout = open(log_filename, 'w') # at this point all print() will go to the log file NOT the terminal # sales order process salesOrder() # Post Process Operations # close txt file sys.stdout.close() # close() is manual due to no with keyword # open() with the with keyword ''' page 46 chapter 4 with open(log_filename, 'w') as file: operations here... ''' # change sys.stdout() back to terminal sys.stdout = sys.__stdout__ # Program Class if __name__ == "__main__": # Page 35 main()