使用azure-communication-email时轮询器卡在InProgress状态,如何调试?
问题描述
在Databricks集群中使用azure-communication-email Python包发送邮件,旧版本脚本运行正常,但升级到2023年7月3日发布的1.0.0b2新版本(邮件构造方式变更)后,脚本始终无法完成,poller一直卡在“InProgress”状态。相关代码如下:
from azure.communication.email import EmailClient email_client = EmailClient.from_connection_string(comm_connection_string) display_name = make_user_display_name(username) message = { "content": { "subject": f"Subject", "plainText": f"email body here", }, "recipients": { "to": [ { "address": username, "displayName": display_name } ] }, "senderAddress": "DoNotReply@azurecomm.net", "attachments": [ { "name":save_name, "contentType": "txt", "contentInBase64": file_bytes_b64.decode() } ] } poller = email_client.begin_send(message) result = poller.result()
调试方案
修正附件参数格式
新版本对附件的参数要求更严格:contentType需使用标准MIME类型,将"txt"改为"text/plain"contentInBase64需要传入未解码的base64字节字符串,移除.decode()调用
修改后的附件代码:
"attachments": [ { "name": save_name, "contentType": "text/plain", "contentInBase64": file_bytes_b64 } ]自定义轮询逻辑排查状态
替换直接调用poller.result()的方式,添加状态打印和超时控制,明确是否真的持续卡在InProgress:import time poller = email_client.begin_send(message) timeout = 300 # 设置5分钟超时 start_time = time.time() while not poller.done(): current_status = poller.status() print(f"当前发送状态: {current_status},已耗时: {time.time() - start_time:.2f}秒") if time.time() - start_time > timeout: raise TimeoutError("邮件发送请求超时") time.sleep(5) # 每5秒查询一次状态 result = poller.result() print(f"发送结果: {result}")验证资源权限与状态
登录Azure门户,检查对应的通信服务资源:- 确认邮件发送功能未被限流或暂停
- 查看监控日志,是否有失败请求记录或权限报错
测试极简邮件排除干扰
暂时移除附件、自定义显示名等非必要参数,发送仅包含主题、正文和收件人的极简邮件,确认是否是附加参数导致的兼容性问题。如果极简邮件发送成功,再逐步添加参数排查问题点。检查Databricks网络连通性
在Databricks集群中执行命令,测试是否能正常访问Azure通信服务的邮件端点:curl -v https://<你的通信服务资源名>.communication.azure.com/emails:send?api-version=2023-03-31若无法连通,需确认集群出站网络规则是否允许访问该服务端点。
内容的提问来源于stack exchange,提问作者Nik
相关产品推荐
相关产品推荐

