File size: 1,030 Bytes
d3ebdae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import asyncio
from .streamer import AsyncResponseDataStreamer
from typing import List, AsyncIterator
from aiohttp.web import Request

class StreamManager:
    
    def __init__(self):
        ...
    
    
    
    async def process_streams(self, request:Request, streams_responses: List[AsyncIterator], stream_uids: List[int]):
        
        # create local lock for returning responses to the front-end
        # creates n number of streamers
        # organizes responses
        # logs responses locally
        # returns selected response to the front-end
        
        lock = asyncio.Lock()
        
        streamers = [AsyncResponseDataStreamer(async_iterator=stream, selected_uid=stream_uid, lock=lock) for stream, stream_uid in zip(streams_responses, stream_uids)]            
        completed_streams = await asyncio.gather(*[streamer.stream(request) for streamer in streamers])
        
        lock.release()
        print(f"Stream {stream_uids} completed the operation.")