I have a class which contains multiple asyinc methods and I want to create dependencies among them. Dependency Graph In this picture both heartbeat and neighborhood_check depend on radio and sub depend on neighborhood_check, that means after starting the radio I want to start heartbeat and neighborhood_check. And after starting neighborhood_check I want to start the sub. I also have a asyinc_start function which start the methods so here I want to manage those but I can't.
Give me some suggestion.
The code of check_dependency.py is as follows:
import asyncio
import sys
import os
from util import run`enter code here`
class ChkDependency():
async def radio(self):
print("Radio is initialized")
await asyncio.sleep(2)
async def pub(self):
await asyncio.sleep(2)
print("pub is initialized")
async def heartbeat(self):
print("heartbeat started")
await asyncio.sleep(2)
async def neigh_hood_check(self):
await asyncio.sleep(2)
print("checking for matches in neigh list")
async def subs(self):
await asyncio.sleep(2)
print("match found")
await asyncio.sleep(2)
print("subscribing.....")
if __name__ == '__main__':
chkdependency = ChkDependency()
async def start():
while True:
await asyncio.wait([
chkdependency.radio(),
chkdependency.pub(),
chkdependency.heartbeat(),
chkdependency.neigh_hood_check(),
chkdependency.subs(),
])
try:
run(
start(),
)
except KeyboardInterrupt:
print("Exiting...")
exit()
The code of util.py is as follows:
import asyncio
def run(*args):
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*args))
I was thinking of implementing the dependency using semaphore but there were no positive results. Please help !!