如果您在沒有適當提示的情況下執行託管在 AgentCore 上的代理,您將浪費您的 Bedrock 配額。
如果你沒有像佐佐木璐希那樣快速完成的特工,你最終會像佐佐木一樣浪費代幣。你還需要一個配額來執行下一個特工…
在這種情況下,您可以停止正在執行的代理程式嗎?
我製作了這種代理商……或者更確切地說,是一種玩具程式碼。
import asyncio
from bedrock_agentcore import BedrockAgentCoreApp
app = BedrockAgentCoreApp()
current_task = None
@app.async_task
async def background_work():
for i in range(3600):
print(f"{i+1}")
await asyncio.sleep(1)
return "Game Over"
@app.entrypoint
async def handler(event):
global current_task
if "prompt" in event:
print("ガハハハ Bedrock Quota を食い尽くしてやる!!")
current_task = asyncio.create_task(background_work())
return {"status": "started"}
if "kill" in event and current_task:
print("◯してやるーー!!!")
current_task.cancel()
current_task = None
print("◯してやったぜ!!!")
return {"status": "killed"}
if __name__ == "__main__":
app.run()
假設該過程將是非同步的(您可能想要停止的長時間執行的過程很可能是非同步的),正在執行的任務儲存在全域變數(current_task)中,並且當收到kill
鍵的請求時,current_task 會被取消。
為了檢查非同步進程是否真的停止了,它會每秒計數並列印(=將其寫入 CWL),如果它沒有停止,它將繼續執行 3600 秒 = 1 小時,遊戲結束。
AgentCore 觸發如下:
import boto3
import json
client = boto3.client('bedrock-agentcore')
input_text = json.dumps({"prompt": "test"})
response = client.invoke_agent_runtime(
agentRuntimeArn="<your agentcore runtime arn>",
payload=input_text
)
print(response)
這將開始計數。
這是停止它的程式碼
response = client.invoke_agent_runtime(
agentRuntimeArn="<your agentcore runtime arn>",
runtimeSessionId=response["runtimeSessionId"],
payload=json.dumps({"kill": "kill"})
)
print(response)
現在,讓我們來看看日誌。
2025-08-21T08:55:52.348Z
INFO: Started server process [1]
2025-08-21T08:55:52.348Z
INFO: Waiting for application startup.
2025-08-21T08:55:52.348Z
INFO: Application startup complete.
2025-08-21T08:55:52.348Z
INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
2025-08-21T08:55:52.520Z
INFO: 127.0.0.1:36582 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:26.226Z
ガハハハ Bedrock Quota を食い尽くしてやる!!
2025-08-21T08:56:26.226Z
INFO: 127.0.0.1:36590 - "POST /invocations HTTP/1.1" 200 OK
2025-08-21 08:56:26,224 - bedrock_agentcore.app - INFO - [3beb8561] Invocation completed successfully (0.000s)
2025-08-21T08:56:26.226Z
2025-08-21 08:56:26,225 - bedrock_agentcore.app - INFO - [3beb8561] Async task started: background_work (ID: 8876725457114168676)
2025-08-21T08:56:27.883Z
1
2025-08-21T08:56:27.883Z
2
2025-08-21T08:56:27.883Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:29.883Z
3
2025-08-21T08:56:29.883Z
4
2025-08-21T08:56:29.883Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:31.884Z
5
2025-08-21T08:56:31.884Z
6
2025-08-21T08:56:31.884Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:33.886Z
7
2025-08-21T08:56:33.886Z
8
2025-08-21T08:56:33.886Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:35.887Z
9
2025-08-21T08:56:35.887Z
10
2025-08-21T08:56:35.887Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:37.668Z
2025-08-21 08:56:37,668 - bedrock_agentcore.app - INFO - [a27b9ef9] Invocation completed successfully (0.000s)
2025-08-21T08:56:37.668Z
11
2025-08-21T08:56:37.668Z
12
2025-08-21T08:56:37.668Z
◯してやるーー!!!
2025-08-21T08:56:37.668Z
◯してやったぜ!!!
2025-08-21T08:56:37.668Z
INFO: 127.0.0.1:58342 - "POST /invocations HTTP/1.1" 200 OK
2025-08-21 08:56:37,669 - bedrock_agentcore.app - INFO - [3beb8561] Async task completed: background_work (ID: 8876725457114168676, Duration: 11.44s)
2025-08-21T08:56:37.888Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:39.889Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:41.890Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:43.891Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:45.895Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:47.893Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:49.894Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:51.895Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:53.896Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
您可以看到計數開始,然後倒數停止!
如果使用全域變數定義任務,則可以從任何地方執行它。
別擔心,我會這麼做的。