我有一个包含多个 asyinc 方法的类,我想在它们之间创建依赖关系。 依赖关系图 在这张图中,heartbeat 和neighbor_check 都依赖于radio,而sub 依赖于neighbor_check,这意味着在启动radio 后我想启动heartbeat 和neighbor_check。在开始neighbour_check 之后,我想启动sub。我还有一个 asyinc_start 函数来启动这些方法,所以我想在这里管理这些方法,但我不能。
给我一些建议。
check_dependency.py 的代码如下:
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()
util.py 的代码如下:
import asyncio
def run(*args):
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*args))
我正在考虑使用信号量来实现依赖,但没有积极的结果。请帮忙 !!