admin管理员组文章数量:1289581
I'm facing an odd behavior on one of my applications. So I have an application that has an POST
entrypoint that receives a list of order id's and after processing each order it must inform an external API about the monetary value of each order. So what we did was the following:
order_details_tasks = [
asyncio.create_task(
self.launch_individual_order_details(order),
name=f"task_order_{order.id}",
)
for order in active_orders
]
results = await asyncio.gather(*order_details_tasks, return_exceptions=True)
for task, result in zip(order_details_tasks, results):
if isinstance(result, Exception):
print(f"⚠️ Task '{task.get_name()}' raised an exception: {result}")
else:
print(f"✅ Task '{task.get_name()}' succeeded with result: {result}")
The launch_individual_order_details(order)
function does the following stuff and other non I/O
logic before this block:
logger.debug(f"Sending order details with success for order: {order.id}")
await order_service.send_order_request(order)
Inside send_order_request
we create a entry on a table called Transaction
with the order id and the corresponding order amount and in pending state and send http request using aiohttp.CLient
library. Afterwards we update the transaction status to Success if the request response is succesful and to error if the request fails.
So the problem we are facing is that when our system is with a relative amount of load in our pods when the pod uses 70% of the CPU limits we give to them we notice that some of our tasks simply break execution and don't inform the event loop about any possible error.
We discovered this because after some load testing we ran some scripts to check transaction status and noticed that some order id's only had the transaction status to pending so they didn't even execute the request to the external api. Furthermore we noticed that for all the orders that were in this state they didn't appear in the succes task log:
print(f"✅ Task '{task.get_name()}' succeeded with result: {result}").
and they also don't appear on the error task log:
print(f"⚠️ Task '{task.get_name()}' raised an exception: {result}").
Furthermore we also turned the event loop
logs debug to true and don't find any kind of error associated with these tasks. The only thing we found out is that the execution reaches this log:
logger.debug(f"Sending order details with success for order: {order.id}")
and that the transaction state for this order is created with pending
state but is never updated again.
Does anyone have any idea why these cases may be happening?
import asyncio
import aiohttp
async def stack_overflow_script(active_orders):
order_details_tasks = [
asyncio.create_task(
launch_individual_order_details(order),
name=f"task_order_{order}",
)
for order in active_orders
]
results = await asyncio.gather(*order_details_tasks, return_exceptions=True)
for task, result in zip(order_details_tasks, results):
if isinstance(result, Exception):
print(f"⚠️ Task '{task.get_name()}' raised an exception: {result}")
else:
print(f"✅ Task '{task.get_name()}' succeeded with result: {result}")
async def launch_individual_order_details(order):
print(f"Sending round details for play: {order}")
await send_order_request(order)
async def send_order_request(order):
params = dict(order_id=order)
try:
async with aiohttp.ClientSession(
) as session:
async with session.request(
"POST",
url=";,
timeout=10,
json=params
) as resp:
resp_json = await resp.json()
return resp_json
except asyncio.TimeoutError:
return
asyncio.run(stack_overflow_script([2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]))
Above is a minimal reproducable script of what I want to achieve. I don't know if when running this script my problem will occur but what I want to share is that sometimes some of this tasks don't execute and simply vanish from the thread event loop without even giving an error back to the execution.
本文标签: pythonAsyncio coroutine execution stopped and didn39t return any errorStack Overflow
版权声明:本文标题:python - Asyncio coroutine execution stopped and didn't return any error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741479523a2381082.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论