1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
import asyncio import traceback
class SpwanClass():
def __init__(self): pass
@asyncio.coroutine def execute(self, request_class_list): response = [] if request_class_list: @asyncio.coroutine def run(request_class): try: result = yield from request_class.execute() response.append(result) except: print(traceback.format_exc()) try: tasks_list = [] for each_class in request_class_list: tasks_list.append( asyncio.Task(run(each_class)) ) yield from asyncio.gather(*tasks_list) except: print(traceback.format_exc()) return response
class RequestClass1():
def __init__(self): print('RequestClass1.__init()__')
@asyncio.coroutine def execute(self): return "class1_response"
class RequestClass2():
def __init__(self): print('RequestClass2.__init()__')
@asyncio.coroutine def execute(self): return "class2_response"
@asyncio.coroutine def main(): try: request_class_list = [RequestClass1(), RequestClass2()] spwanClass = SpwanClass() final_result = yield from spwanClass.execute(request_class_list) print("final_result:%s"%final_result) except: print(traceback.format_exc())
loop = asyncio.get_event_loop() loop.run_until_complete(main())
|
Reference
잘못된 부분에 대한 지적은 언제든지 감사히 받겠습니다.
rochan87@gmail.com
Related Posts
Python- yield, generator, coroutine
Python - Python?