1

我正在尝试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()
4

1 回答 1

1

在对在 Django 中运行异步方法这一主题进行了更多研究之后,我找到了一个在我的案例中真正有效的解决方案。

我找到的解决方案是CELERY。我从这里找到了这个。Celery 与 Django 一起工作得非常好。

于 2020-07-24T13:19:51.040 回答