好的,所以首先有一个误会.这个
async def getPdf(request):
asyncio.create_task(renderPdfsInExecutor(request.json))
...
async def renderPdfsInExecutor(json):
asyncio.get_running_loop.run_in_executor(executor, syncRenderPdfs, json)
是多余的.这足以做
async def getPdf(request):
asyncio.get_running_loop.run_in_executor(executor, syncRenderPdfs, request.json)
...
或者(因为您不想等待)甚至更好
async def getPdf(request):
executor.submit(syncRenderPdfs, request.json)
...
现在,您遇到的问题是因为s??yncRenderPdfs抛出PermissionError.它没有被处理,因此Python警告您“嘿,某些后台代码引发了错误.但是该代码不归任何人所有,这到底是什么?”.这就是为什么您从未获取Future异常的原因.您对pdf库本身有问题,而不是asyncio有问题.一旦解决了内在问题,安全起来也是一个好主意:
def syncRenderPdfs(json)
try:
#Some PDF Library that downloads images synchronously
pdfs = somePdfLibrary.generatePdfsFromJson(json)
sendToDefaultMail(pdfs)
except Exception:
logger.exception('Something went wrong') # or whatever
您的“权限被拒绝”问题完全是另外一回事,您应该对其进行调试和/或发布一个单独的问题.
至于最后一个问题:是的,执行者将在工作人员之间排队并平均分配任务.
编辑:正如我们在评论中所述,实际的问题可能与您使用的Windows环境有关.或更确切地说,使用ProcessPoolExecutor,即,生成程序可能会更改权限.我建议使用ThreadPoolExecutor,假设它在平台上可以正常工作.