You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there any recommended mechanism for cancelling an ongoing deep research task?
I have set up a Flask API that starts research tasks when hit, but I want to enable the ability to cancel research on the server via an endpoint thats hit by a cancel button.
I come from Typescript/JS world, so sorry if this is a stupid question with an obvious answer in Python. LLM is recommending not to use any tool exposed by the library, but to use asyncio's task.cancel(). It says it will cancel the next time an await is hit inside the package.
Is there a more correct way to do this, or would this be the standard approach?
Basic idea:
importasynciofromgpt_researcherimportGPTResearchertasks= {}
# invoked by research endpoint, put into tasks varasyncdefgenerate_report(query: str) ->str:
researcher=GPTResearcher(query, "deep")
research_task=asyncio.create_task(researcher.conduct_research())
## additional code# Endpoint to cancel a background task by ID@app.route('/cancel/<int:task_id>', methods=['POST'])defcancel_task(task_id):
task=tasks.get(task_id)
iftaskisNone:
returnjsonify({"error": f"No task with ID {task_id} found."}), 404task.cancel()
returnjsonify({"message": f"Cancellation requested for task {task_id}."})
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there any recommended mechanism for cancelling an ongoing deep research task?
I have set up a Flask API that starts research tasks when hit, but I want to enable the ability to cancel research on the server via an endpoint thats hit by a cancel button.
I come from Typescript/JS world, so sorry if this is a stupid question with an obvious answer in Python. LLM is recommending not to use any tool exposed by the library, but to use
asyncio
'stask.cancel()
. It says it will cancel the next time anawait
is hit inside the package.Is there a more correct way to do this, or would this be the standard approach?
Basic idea:
Beta Was this translation helpful? Give feedback.
All reactions