"""
Mock up a video feed pipeline
"""
import asyncio
import logging
import sys

import cv2

logging.basicConfig(format="[%(thread)-5d]%(asctime)s: %(message)s")
logger = logging.getLogger('async')
logger.setLevel(logging.INFO)


async def process_video(filename):
    cap = cv2.VideoCapture(filename)
    tasks = list()
    frame_ind = 0
    while cap.isOpened():
        ret, frame = cap.read()
        tasks.append(asyncio.ensure_future(process_frame(frame, frame_ind)))
        frame_ind += 1
        await asyncio.sleep(0)
    await asyncio.gather(tasks)


async def process_frame(frame, frame_ind):
    logger.info("Processing frame {}".format(frame_ind))
    await asyncio.sleep(20.0)
    logger.info("Finished processing frame {}".format(frame_ind))


def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(process_video(sys.argv[1]))
    logger.info("Completed")


if __name__ == '__main__':
    main()

[((92, 160), 'logging.basicConfig', 'logging.basicConfig', ([], {'format': '"""[%(thread)-5d]%(asctime)s: %(message)s"""'}), "(format='[%(thread)-5d]%(asctime)s: %(message)s')\n", (111, 160), False, 'import logging\n'), ((170, 196), 'logging.getLogger', 'logging.getLogger', (['"""async"""'], {}), "('async')\n", (187, 196), False, 'import logging\n'), ((274, 300), 'cv2.VideoCapture', 'cv2.VideoCapture', (['filename'], {}), '(filename)\n', (290, 300), False, 'import cv2\n'), ((782, 806), 'asyncio.get_event_loop', 'asyncio.get_event_loop', ([], {}), '()\n', (804, 806), False, 'import asyncio\n'), ((537, 558), 'asyncio.gather', 'asyncio.gather', (['tasks'], {}), '(tasks)\n', (551, 558), False, 'import asyncio\n'), ((671, 690), 'asyncio.sleep', 'asyncio.sleep', (['(20.0)'], {}), '(20.0)\n', (684, 690), False, 'import asyncio\n'), ((510, 526), 'asyncio.sleep', 'asyncio.sleep', (['(0)'], {}), '(0)\n', (523, 526), False, 'import asyncio\n')]