Guest User

AsyncIO Pool attempt

a guest
Aug 14th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. import asyncio
  2. import random
  3.  
  4.  
  5. PARALLEL = 3
  6.  
  7.  
  8. session_pool = []
  9. pool_queue = asyncio.Queue()
  10. for i in range(PARALLEL):
  11.     session_pool.append({
  12.         'place': None,
  13.         # some other data here
  14.     })
  15.  
  16.  
  17. @asyncio.coroutine
  18. def pool_job(coro, chosenN):
  19.     r = yield from coro
  20.     session_pool[chosenN]['place'] = None
  21.     print('Free {}'.format(chosenN))
  22.     return r
  23.  
  24.  
  25. def pick_free_pool_place():
  26.     for i in range(PARALLEL):
  27.         if session_pool[i]['place'] is None:
  28.             return i
  29.     return None
  30.  
  31.  
  32. @asyncio.coroutine
  33. def pool_manager():
  34.     while True:
  35.         coro = yield from pool_queue.get()
  36.  
  37.         chosenN = pick_free_pool_place()
  38.         if chosenN is None:
  39.             yield from asyncio.wait(
  40.                 [r['place'] for r in session_pool if r['place'] is not None],
  41.                 return_when=asyncio.FIRST_COMPLETED
  42.             )
  43.             chosenN = pick_free_pool_place()
  44.  
  45.         sr = session_pool[chosenN]
  46.  
  47.         task = asyncio.async(pool_job(coro, chosenN))
  48.         sr['place'] = task
  49.         print('Apply for {}'.format(chosenN))
  50.  
  51.  
  52. @asyncio.coroutine
  53. def main():
  54.     task_count = 0
  55.     quit = asyncio.Event()
  56.  
  57.     @asyncio.coroutine
  58.     def enqueue_test_task(coro):
  59.         nonlocal task_count
  60.         task_count += 1
  61.  
  62.         @asyncio.coroutine
  63.         def f():
  64.             nonlocal task_count
  65.             yield from coro
  66.             task_count -= 1
  67.             if task_count <= 0:
  68.                 quit.set()
  69.  
  70.         yield from pool_queue.put(f())
  71.  
  72.     for i in range(10):
  73.         yield from enqueue_test_task(asyncio.sleep(random.randint(1, 4)))
  74.  
  75.     yield from quit.wait()
  76.  
  77.  
  78. loop = asyncio.get_event_loop()
  79. asyncio.async(pool_manager())
  80. loop.run_until_complete(main())
  81. loop.close()
Advertisement
Add Comment
Please, Sign In to add comment