2

我正在 python 中开发 TimerTrigger Azure 函数,该函数大量使用 aiohttp 库向文件缓存发出并发请求,获取约 8K JSON 文件,并准备将它们加载到数据库中。我已经能够在我的本地机器(OSX)上端到端地运行该过程而不会出现问题。也就是说,使用 Azure Functions Core Tools,我已经能够func start处理,通过 POST 请求开始工作http://localhost:7071/admin/functions/NameOfMyFunction,并且一切正常。

但是,当我将此函数发布到我的 Azure Functions 应用程序时,TimerTrigger 会按预期启动,但在“同时获取 JSON 文件”的过程中不太远的地方,函数执行失败并出现此错误(我已经编辑了出于保密原因,我正在访问的实际 url 和 IP 地址):

Result: Failure Exception: ClientConnectorError: Cannot connect to host https://FILE-CACHE-URL:443 ssl:default [Connect call failed ('XX.XXX.XXX.XXX', 443)] Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 370, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions

以下是我正在运行的实际代码的一些关键摘录

run.py Azure Function 的入口点

import asyncio
import azure.functions as func

from helpers.doctor_info import fetch_doctor_profiles


def main(myTimer: func.TimerRequest) -> None:
           
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    doc_profiles = loop.run_until_complete(fetch_doctor_profiles())

doctor_info.py文件中,导入的辅助函数获取配置文件:给定我需要从缓存中获取的大约 8K 文件的大列表,这会将它们分成 50 个批次,并同时从每个批次中获取文件,允许中间停顿。

async def fetch_doctor_profiles(batch_size = 50, max_concurrent_requests = 15,
    use_trust_env = True):
   
    json_list = []
    file_paths = get_cache_file_paths_from_manifest()
    path_batches = make_batches_of_paths(paths = file_paths, size = batch_size)
    sem = asyncio.Semaphore(max_concurrent_requests)
    connector = aiohttp.TCPConnector(verify_ssl=False)

    async with ClientSession(connector = connector, trust_env = use_trust_env) as session:
        json_batches = await asyncio.gather(*[fetch_jsons_in_batch(sem, session, batch) \
            for batch in path_batches])
    for jsons in json_batches:
        unpack_fetched_profiles(profile_list = jsons, out_list = json_list)
    return json_list

正如您可能在上面的摘录中看到的那样,我最初认为这可能是 SSL 握手问题,并尝试禁用 SSL 验证但没有运气:托管在我的笔记本电脑上时一切正常,但在 Azure 中中断.

鉴于这在本地总是可以正常工作,但在部署中从来没有工作过,我认为这个问题的根源是一旦这个过程托管在云中,环境就会有所不同,但我对如何准确诊断出这种差异是什么?

很高兴提供更多细节,但认为这足以开始。非常感谢,任何帮助将不胜感激!

4

0 回答 0