利用dask发送并行API请求及错误处理

2024-10-03 15:33:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近开始用dask。我想用http请求将数据发送到restapi,API返回一个json文件来验证数据上传是否成功。下面是我的API调用函数:

def requestToAPI():
    headers={'Content-Type': 'application/json'}
    data = {
      "api_key" : "xxxxxxxxxxxxx",
      "attributes" : [
       {
         "external_id" : "user1",
         "app_id" : "xxxx-xxx-xxxxx-xxxx",
         "firs_name" : "user_firstname",
         "last_name" : "user_lastname_test"
       }
     ]
    }
    r = requests.post('https://abcdf.com/users/abdcgdu', headers=headers, data=json.dumps(data))
    return r.json()

我有许多dask数据帧块,我从下面的代码中得到:

^{pr2}$

如何使用dask和上面的块(假设每个块都会变成正确的json文件)来向API发送并行请求,并在其中一个请求失败/返回错误时进行正确的错误处理?在

我试着用达斯克。延迟公司名称:

[对于rChunk中的块,延迟(requestToAPI)(chunk)]

但我不知道如何正确处理错误??在


Tags: 文件数据nameapirestapiidjsonhttp
1条回答
网友
1楼 · 发布于 2024-10-03 15:33:02

我不确定dask数据帧是否是您应用程序的最佳选择。你可能想看看延迟的,未来的,或包API。在

我可能会用同期期货在

from dask.distributed import Client, as_completed

futures = client.map(process, requests)
for future in as_completed(futures):
    try:
        response = future.result()
        # do stuff with result
    except Exeption:
        # do stuff

http://docs.dask.org/en/latest/futures.html

相关问题 更多 >