view CHAP/async.py @ 10:7cc954f15149 draft

planemo upload for repository https://github.com/CHESSComputing/ChessAnalysisPipeline/tree/galaxy commit 2d65c72740d0439b8385378a890b3763d717f152-dirty
author kls286
date Wed, 29 Mar 2023 17:53:07 +0000
parents cbbe42422d56
children
line wrap: on
line source

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#pylint: disable=
"""
File       : async.py
Author     : Valentin Kuznetsov <vkuznet AT gmail dot com>
Description: AsyncProcessor module
"""

# system modules
import asyncio

# local modules
from CHAP.processor import Processor, PrintProcessor


async def task(mgr, doc):
    """
    Process given data using provided task manager
    """
    return mgr.process(doc)


async def executeTasks(mgr, docs):
    """
    Process given set of documents using provided task manager
    """
    coRoutines = [task(mgr, d) for d in docs]
    await asyncio.gather(*coRoutines)


class AsyncProcessor(Processor):
    """
    AsyncProcesor process given data via asyncio module
    """
    def __init__(self, mgr):
        super().__init__()
        self.mgr = mgr

    def _process(self, docs):
        """
        Internal method to process given data documents
        """
        asyncio.run(executeTasks(self.mgr, docs))

def example():
    """
    Helper function to demonstrate usage of AsyncProcessor
    """
    docs = [1,2,3]
    mgr = PrintProcessor()
    processor = AsyncProcessor(mgr)
    processor.process(docs)

if __name__ == '__main__':
    example()