0

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

RuntimeError: Task <Task pending name='Task-4' coro=<stop_trigger() running at /home/atlevesque/Sources/CustomDevopsScripts/trigger_pythonTests.py:37>> 将 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 没有意义......)

在此先感谢您的帮助!

4

1 回答 1

0

您可以尝试以下任一方法来解决此 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和RuntimeError: Task got Future <Future pending> 附加到不同的循环

于 2022-02-24T06:52:19.737 回答