gbe*_*ven 8 python asynchronous salesforce python-3.x aiohttp
我编写了一个Python 3.7脚本,该脚本使用单个语句查询的多个对象异步(asyncio 3.4.3 and aiohttp 3.5.4)创建Salesforce批量 API(v45.0)作业/批处理SOQL,等待批处理完成,完成后将结果下载(流式传输)到服务器,进行一些数据转换,以及然后最后将结果同步上传到SQL Server 2016 SP1 (13.0.4560.0). 我已经进行了很多成功的试运行,并认为它运行良好,但是,我最近开始间歇性地收到以下错误,并且对如何修复感到不知所措,因为很少有关于此的报告/解决方案在网上:
aiohttp.client_exceptions.ClientPayloadError:响应负载未完成
示例代码片段:
import asyncio,aiohttp,aiofiles
from simple_salesforce import Salesforce
from xml.etree import ElementTree
#Establish a session using the simple_salesforce module
sf = Salesforce(username=username,
password=password,
security_token=securityToken,
organizationId=organizationId)
sfAPIURL = 'https://myinstance.salesforce.com/services/async/45.0/job/'
sfDataPath = 'C:/Salesforce/Data/'
#Dictionary to store information for the object/job/batch while the script is executing
objectDictionary =
{'Account': {'job':
{'batch': {'id': '8596P00000ihwpJulI','results': ['8596V00000Bo9iU'],'state': 'Completed'},
'id': '8752R00000iUjtReqS'},
'soql': 'select Id,Name from Account'},
'Contact': {'job':
{'batch': {'id': '9874G00000iJnBbVgg','results': ['7410t00000Ao9vp'],'state': 'Completed'},
'id': '8800o00000POIkLlLa'},
'soql': 'select Id,Name from Contact'}}
async def retrieveResults(jobId, batchId, sfObject):
headers = {"X-SFDC-Session": sf.session_id, 'Content-Encoding': 'gzip'}
async with aiohttp.ClientSession() as session:
async with session.get(url=f'{sfAPIURL}{jobId}/batch/{batchId}/result', headers=headers) as r:
data = await r.text()
batchResults = ElementTree.fromstring(data) #list of batch results
for resultID in batchResults:
async with session.get(url=f'{sfAPIURL}{jobId}/batch/{batchId}/result/{resultID.text}', headers=headers, timeout=None) as r:
async with aiofiles.open(f'{sfDataPath}{sfObject}_TEMP_JOB_{jobId}_BATCH_{batchId}_RESULT_{resultID.text}.csv', 'wb') as outfile: #save in temporary file for manipulation later
while True:
chunk = await r.content.read(81920)
if not chunk:
break
await outfile.write(chunk)
async def asyncDownload():
await asyncio.gather(*[retrieveResults(objectDictionary[sfObject]['job']['id'], objectDictionary[sfObject]['job']['batch']['id'], sfObject) for sfObject in objectDictionary])
if __name__ == "__main__":
asyncio.run(asyncDownload())
Run Code Online (Sandbox Code Playgroud)
回溯(错误行与上面的代码片段不匹配):
回溯(最近一次调用最后一次):
文件“C:\Code\salesforce.py”,第 252 行,在 asyncio.run(asyncDownload()) 中
文件“C:\Program Files\Python37\lib\asyncio\runners.py”,第 43 行,在运行中 return loop.run_until_complete(main)
文件“C:\Program Files\Python37\lib\asyncio\base_events.py”,第 584 行,在 run_until_complete 中返回 future.result()
文件“C:\Code\salesforce.py”,第 241 行,在 asyncDownload await asyncio.gather(*[retrieveResults(objectDictionary[sfObject]['job']['id'], objectDictionary[sfObject]['job'] ['batch']['id'], sfObject) for sfObject in objectDictionary])
文件“C:\Code\salesforce.py”,第 183 行,在retrieveResults chunk = await r.content.read(81920)
文件“C:\Program Files\Python37\lib\site-packages\aiohttp\streams.py”,第 369 行,在 read await self._wait('read')
文件“C:\Program Files\Python37\lib\site-packages\aiohttp\streams.py”,第 297 行,在 _wait await waiter
aiohttp.client_exceptions.ClientPayloadError:响应负载未完成
问题的根源似乎始于r.content.read(81920)哪个应该是 81920 字节块中的流数据,但这是我所能得到的。
我不认为这是我的网络问题,因为还有其他小作业连接到此服务器上的外部源,在此作业运行时可以毫无问题地完成。有谁知道这里发生了什么?
谢谢!
-编辑:
我试过iter_any()而不是read()仍然得到同样的错误......
async for data in r.content.iter_any():
await outfile.write(data)
Run Code Online (Sandbox Code Playgroud)
我试过了readline(),仍然得到同样的错误......
async for line in r.content.readline():
await outfile.write(line)
Run Code Online (Sandbox Code Playgroud)
从那以后,我在代码的错误处理部分(未包含在原始问题中)中使用了一些重试功能,最终允许作业完成。负载错误仍在发生,这仍然是主要问题,但重试下载是一个成功的解决方法。如果有人能够提供更多信息,问题仍然存在。
小智 0
您好,您是否尝试在以下位置插入await asyncio.sleep(0):
...
while True:
chunk = await r.content.read(81920)
await asyncio.sleep(0)
if not chunk:
break
await outfile.write(chunk)
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4313 次 |
| 最近记录: |