comparison build/lib/CHAP/async.py @ 0:cbbe42422d56 draft

planemo upload for repository https://github.com/CHESSComputing/ChessAnalysisPipeline/tree/galaxy commit 1401a7e1ae007a6bda260d147f9b879e789b73e0-dirty
author kls286
date Tue, 28 Mar 2023 15:07:30 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cbbe42422d56
1 #!/usr/bin/env python
2 #-*- coding: utf-8 -*-
3 #pylint: disable=
4 """
5 File : async.py
6 Author : Valentin Kuznetsov <vkuznet AT gmail dot com>
7 Description: AsyncProcessor module
8 """
9
10 # system modules
11 import asyncio
12
13 # local modules
14 from CHAP.processor import Processor, PrintProcessor
15
16
17 async def task(mgr, doc):
18 """
19 Process given data using provided task manager
20 """
21 return mgr.process(doc)
22
23
24 async def executeTasks(mgr, docs):
25 """
26 Process given set of documents using provided task manager
27 """
28 coRoutines = [task(mgr, d) for d in docs]
29 await asyncio.gather(*coRoutines)
30
31
32 class AsyncProcessor(Processor):
33 """
34 AsyncProcesor process given data via asyncio module
35 """
36 def __init__(self, mgr):
37 super().__init__()
38 self.mgr = mgr
39
40 def _process(self, docs):
41 """
42 Internal method to process given data documents
43 """
44 asyncio.run(executeTasks(self.mgr, docs))
45
46 def example():
47 """
48 Helper function to demonstrate usage of AsyncProcessor
49 """
50 docs = [1,2,3]
51 mgr = PrintProcessor()
52 processor = AsyncProcessor(mgr)
53 processor.process(docs)
54
55 if __name__ == '__main__':
56 example()