#Concurrent(Allows to deal with I/O while computing) print('------- Concurrent Programming in Python --------') import asyncio
#This library will work only from python3.5 @asyncio.coroutine def countdown(number, n): while n > 0: print('T-minus', n, '({})'.format(number)) yield from asyncio.sleep(1) n -= 1 loop = asyncio.get_event_loop() tasks = [ asyncio.ensure_future(countdown("A", 5)), asyncio.ensure_future(countdown("B", 5)), asyncio.ensure_future(countdown("c", 5))] loop.run_until_complete(asyncio.wait(tasks)) loop.close()
#Parallel(helps to increase the throughput of the machine) print('------- Parallel Programming in Python --------') from multiprocessing.dummy import Pool as ThreadPool
#Above library will work only from python3.5 import time def squareNumber(nn): time.sleep(5) for i in range(nn): print (i) pool = ThreadPool(2) #by increasing the thread count(2),it will create a new thread to handle parallel execution result=pool.map(squareNumber, [2,4,6]) pool.close() pool.join()