in Education by
This question already has answers here: asynchronous python itertools chain multiple generators (2 answers) Closed 2 years ago. I would like to listen for events from multiple instances of the same object and then merge this event streams to one stream. For example, if I use async generators: class PeriodicYielder: def __init__(self, period: int) -> None: self.period = period async def updates(self): while True: await asyncio.sleep(self.period) yield self.period I can successfully listen for events from one instance: async def get_updates_from_one(): each_1 = PeriodicYielder(1) async for n in each_1.updates(): print(n) # 1 # 1 # 1 # ... But how can I get events from multiple async generators? In other words: how can I iterate through multiple async generators in the order they are ready to produce next value? async def get_updates_from_multiple(): each_1 = PeriodicYielder(1) each_2 = PeriodicYielder(2) async for n in magic_async_join_function(each_1.updates(), each_2.updates()): print(n) # 1 # 1 # 2 # 1 # 1 # 2 # ... Is there such magic_async_join_function in stdlib or in 3rd party module? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can use wonderful aiostream library. It'll look like this: import asyncio from aiostream import stream async def test1(): for _ in range(5): await asyncio.sleep(0.1) yield 1 async def test2(): for _ in range(5): await asyncio.sleep(0.2) yield 2 async def main(): combine = stream.merge(test1(), test2()) async with combine.stream() as streamer: async for item in streamer: print(item) asyncio.run(main()) Result: 1 1 2 1 1 2 1 2 2 2

Related questions

0 votes
    I am confused about the usage of string.join(list) instead of list.join(string), for instance see this code: ... reason behind this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I've always laughed to myself when I've looked back at my VB6 days and thought, "What modern language doesn't ... ++/-- notation? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Is there a way to get functionality similar to mkdir -p on the shell from within Python. I am looking for a ... has already written it? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    Hi, I am a relatively new programmer and my teacher gave us this problem to fix. Thing is, I have no idea ... wrong with this problem? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I had some Python 2 code as follows (pardon the indentation): def getZacksRating(symbol): c = httplib.HTTPSConnection("www. ... data.split(' ')[1] result = ratingPart.partition("...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    URL is http://*.*.*.*/100/?id=1&version=1 params is {"cityId": "110000", "query": {" ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I ... to this problem look like? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    What are generators in Python?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    This question already has answers here: Pandas Merging 101 (6 answers) Closed 3 years ago. Sorry guys, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Pandas Merging 101 (6 answers) Closed 3 years ago. Sorry guys, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Forming a matrix by summing 2 arrays element-array wise using NumPy ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Forming a matrix by summing 2 arrays element-array wise using NumPy ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How do I plot two countplot graphs side by side in seaborn? ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How do I plot two countplot graphs side by side in seaborn? ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I would like to use the .assign method with multiple lambda functions to multiple datasets. So far, I' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
...