【问题标题】:Trying to start several ADf triggers using asyncio with azure python SDK, I get RuntimeError: got Future <Future pending> attached to a different loop尝试使用带有 azure python SDK 的 asyncio 启动多个 ADf 触发器,我得到 RuntimeError: got Future <Future pending> attach to a different loop
【发布时间】:2022-02-24 14:52:19
【问题描述】:

我正在尝试编写一个简单的 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 没有意义......)

提前感谢您的帮助!

【问题讨论】:

    标签: python azure azure-data-factory azure-python-sdk


    【解决方案1】:

    您可以尝试以下任一方法来解决此 RuntimeError: Task &lt;Task pending name='Task-4' coro=&lt;stop_trigger() running at /home/atlevesque/Sources/CustomDevopsScripts/trigger_pythonTests.py:37&gt;&gt; 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> attached to a different loop

    【讨论】:

      猜你喜欢
      • 2023-02-14
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      相关资源
      最近更新 更多