fork download
  1. FAILED_PROCESS = "Process 3"
  2.  
  3. class ProcessInitError(Exception):
  4. pass
  5.  
  6. class Process:
  7. def __init__(self, name):
  8. if name == FAILED_PROCESS:
  9. raise ProcessInitError("Something goes wrong with {name}".format(name=name))
  10. self.name = name
  11. print(self.name, "created")
  12.  
  13. def stop(self):
  14. print(self.name, "stopped")
  15.  
  16. class ProcessManager:
  17. def __init__(self):
  18. self.processes = []
  19.  
  20. def init(self, name):
  21. process = Process(name)
  22. self.processes.append(process)
  23.  
  24. def release_all(self):
  25. for process in reversed(self.processes):
  26. process.stop()
  27.  
  28.  
  29. pm = ProcessManager()
  30. try:
  31. pm.init("Process 1")
  32. pm.init("Process 2")
  33. pm.init("Process 3")
  34. except ProcessInitError as err:
  35. print(err)
  36. pm.release_all()
  37.  
Runtime error #stdin #stdout #stderr 0.02s 28384KB
stdin
Standard input is empty
stdout
Process 1 created
Process 2 created
Something goes wrong with Process 3
Process 2 stopped
Process 1 stopped
stderr
Traceback (most recent call last):
  File "./prog.py", line 33, in <module>
    pm.init("Process 3")
  File "./prog.py", line 21, in init
    process = Process(name)
  File "./prog.py", line 9, in __init__
    raise ProcessInitError("Something goes wrong with {name}".format(name=name))
__main__.ProcessInitError: Something goes wrong with Process 3