我正在尝试asyncio在我的代码中使用。这是可行的,但问题是我希望我的代码继续执行,而不是继续等待asyncio代码完成执行。
在当前场景中,我的代码一直等待 asyncio 代码result = asyncio.run(run())完成,然后再进入下一行代码。
我认为我的 asyncio 代码需要在新线程上执行。但是我怎样才能让它工作呢?
我正在使用 Django。
def Dashboard(requests):
result = asyncio.run(run())
all_Farm = MyCollection.objects.all().filter(userid=requests.user.id)
return render(requests, 'Dashboard.html',
{'all_farm': all_Farm})
async def run():
drone = System()
await drone.connect(system_address="udp://:14540")
print("Waiting for drone to connect...")
async for state in drone.core.connection_state():
if state.is_connected:
print(f"Drone discovered with UUID: {state.uuid}")
break
print("Waiting for drone to have a global position estimate...")
async for health in drone.telemetry.health():
if health.is_global_position_ok:
print("Global position estimate ok")
break
print("-- Arming")
await drone.action.arm()
print("-- Taking off")
await drone.action.takeoff()
await asyncio.sleep(5)
print("-- Landing")
await drone.action.land()