Guest User

Untitled

a guest
Oct 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. import asyncio
  2. import sys
  3. import logging
  4.  
  5. logger = logging.getLogger()
  6. logger.setLevel(logging.DEBUG)
  7. sh = logging.StreamHandler()
  8. sh.setLevel(logging.DEBUG)
  9. logger.addHandler(sh)
  10.  
  11.  
  12. def custom_exception_handler(loop, context):
  13. loop.default_exception_handler(context); sys.stdout.flush()
  14. loop.stop()
  15. # exception = context.get('exception')
  16. # raise exception
  17.  
  18.  
  19. loop = asyncio.get_event_loop()
  20. loop.set_exception_handler(custom_exception_handler)
  21.  
  22.  
  23. class A:
  24. async def wait_and_fail(self):
  25. await asyncio.sleep(1)
  26. raise ValueError
  27.  
  28. async def run(self, how_to_set: str):
  29. logger.info('task = asyncio.get_event_loop().create_task(self.wait_and_fail())')
  30.  
  31. if how_to_set == 'field':
  32. self.task = asyncio.get_event_loop().create_task(self.wait_and_fail())
  33. elif how_to_set == 'none':
  34. asyncio.get_event_loop().create_task(self.wait_and_fail())
  35. elif how_to_set == 'var':
  36. task = asyncio.get_event_loop().create_task(self.wait_and_fail())
  37. elif how_to_set == 'list':
  38. [asyncio.get_event_loop().create_task(self.wait_and_fail())]
  39. elif how_to_set == 'list_field':
  40. self.tasks = [asyncio.get_event_loop().create_task(self.wait_and_fail())]
  41. else:
  42. raise ValueError
  43.  
  44. for i in range(5):
  45. await asyncio.sleep(1)
  46. logger.info(f'i = {i}')
  47. logger.info('run success')
  48.  
  49.  
  50. async def run_a_and_del(how_to_set):
  51. logger.info('start run_a_and_del')
  52. a = A()
  53. logger.info('start await a.run(set_task=set_task)')
  54. await a.run(how_to_set=how_to_set)
  55. if hasattr(a, 'task'):
  56. logger.info('start del a.task')
  57. del a.task
  58. if hasattr(a, 'tasks'):
  59. logger.info('start del a.tasks')
  60. del a.tasks
  61. logger.info('start del a')
  62. del a
  63.  
  64.  
  65. async def main():
  66. # different approaches
  67. if 1: # it fails with exception immediately with exit_code=1
  68. a = A()
  69. await a.run(how_to_set='none')
  70. elif 0: # it finish execution and print exception in the end with exit_code=0
  71. a = A()
  72. await a.run(how_to_set='field')
  73. elif 0: # it finish execution, destruct A instance and print exception in the end with exit_code=0
  74. await run_a_and_del(how_to_set='field')
  75. elif 0: # it finish execution and print exception in the end with exit_code=0
  76. a = A()
  77. await a.run(how_to_set='var')
  78. elif 0: # it fails with exception immediately with exit_code=1
  79. a = A()
  80. await a.run(how_to_set='list')
  81. elif 0: # it finish execution and print exception in the end with exit_code=0
  82. a = A()
  83. await a.run(how_to_set='list_field')
  84. logger.info('main finish')
  85.  
  86.  
  87. if __name__ == '__main__':
  88. asyncio.get_event_loop().run_until_complete(main())
Add Comment
Please, Sign In to add comment