mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Request cancellation sample (#459)
* request cancellation via tasks * fix missing kwargs
This commit is contained in:
committed by
GitHub
Unverified
parent
011edfe420
commit
feb4e908ae
@@ -0,0 +1,19 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework.openai import OpenAIChatClient
|
||||
|
||||
async def main():
|
||||
chat_client = OpenAIChatClient()
|
||||
|
||||
try:
|
||||
task = asyncio.create_task(chat_client.get_response(messages=["Tell me a fantasy story."]))
|
||||
await asyncio.sleep(1)
|
||||
task.cancel()
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
print("Request was cancelled")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user