@shared_task(queue='tool_invoke')
def tool_invoke_task_queue(type: str, data: Union[bytes, str], path: str = None):
bucket = 'tools'
try:
if type == 'image':
file = download_and_convert_to_filestorage(data)
data = file.read()
logger.info(f"file.name {file.name}")
# path = f"{path}/{file.name}"
MINIO.put(bucket, path, data)
except Exception as e:
raise ValueError('文件上传失败')
url = MINIO.get_public_url(bucket, path)
url = str(URL(os.getenv('OSS_FILE_PRE')) / url.lstrip("/"))
logger.info(f"文件的url为: {url}")
return url
result = tool.invoke(
user_id="",
tool_parameters=tool_parameters
)
if isinstance(result, ToolInvokeMessage):
result = {**result.dict(), 'type': result.type.value}
if result['type'] not in ['text', 'json','link']:
result = ToolInvokeService.format_result(result, oss_base_path)
elif isinstance(result, list):
result = [ToolInvokeService.format_result(
{**r.dict(), 'type': r.type.value},
oss_base_path
) for r in result]
return result, credentials_schema
@staticmethod
def format_result(result: dict, oss_base_path: str) -> dict:
if result['type'] in ['text', 'json','link']:
return result
if result['type'] == 'blob':
path = f"{oss_base_path}{ToolInvokeService.mime_type_to_extension(result['meta']['mime_type'])}"
elif result['type'] == 'image':
path = oss_base_path
async_result=tool_invoke_task_queue.delay(result['type'], result['message'], path)
url = async_result.get()
print("url",url)
result['message'] = url
return result
当直接执行如下代码时
a=tool_invoke_task_queue.delay(a,b)
result['message']=a
会报错:
sqlalchemy.exc.StatementError: (builtins.TypeError) Object of type AsyncResult is not JSON serializable
因此,需要解决异步任务获取返回值
标签:异步,url,tool,celery,result,返回值,path,type,oss From: https://www.cnblogs.com/Gimm/p/18432591