Trying to start several ADf triggers using asyncio with azure python SDK, I get RuntimeError: got Future <Future pending> attached to a different loop

Trying to start several ADf triggers using asyncio with azure python SDK, I get RuntimeError: got Future <Future pending> attached to a different loop

我正在尝试编写一个简单的 python 脚本来在我的 Azure 数据工厂中启动多个触发器。 但是,当我尝试使用 azure SDK 类 的 aio 变体时,出现以下错误:

RuntimeError: Task > 将 Future 附加到不同的循环

我的脚本如下所示:

import asyncio
from azure.identity.aio import DefaultAzureCredential
from azure.mgmt.datafactory.aio import DataFactoryManagementClient


rg_name = "MY_RG_NAME"
adf_name = "MY_ADF_NAME"
subscription_id = "MY_SUBSCRIPTION_ID"

credentials = DefaultAzureCredential()

adf_client = DataFactoryManagementClient(credentials, subscription_id)

async def start_trigger(adf_client: DataFactoryManagementClient, trigger_name: str, resource_group_name=rg_name, factory_name=adf_name):
    print(f"Starting trigger {trigger_name}")
    await adf_client.triggers.begin_start(resource_group_name=rg_name, factory_name=adf_name, trigger_name=trigger_name)
    print(f"Trigger {trigger_name} started")

async def main(adf_client: DataFactoryManagementClient, triggers_to_start: list):
    trigger_start_tasks = [
        asyncio.create_task(start_trigger(adf_client, trigger))
        for trigger in triggers_to_start
    ]

    for trigger_task in (trigger_start_tasks):
        await trigger_task

triggers_to_start = [
    "Trigger_A",
    "Trigger_B",
]

asyncio.run(
    main(adf_client, triggers_to_start)
)

当我在开始下一个任务之前等待每个任务时,我的脚本实际上工作了(但是使用 aio 没有意义...)

在此先感谢您的帮助!

您可以尝试以下任一方法来解决此 RuntimeError: Task <Task pending name='Task-4' coro=<stop_trigger() running at /home/atlevesque/Sources/CustomDevopsScripts/trigger_pythonTests.py:37>> got Future attached to a different loop 错误:

  1. 添加asyncio.get_event_loop().run_until_complete(main())` or `app.run(main())

  2. 添加loop = asyncio.get_event_loop() app.run(debug=1,loop=loop)

参考文献:https://github.com/pyrogram/pyrogram/issues/391 and