mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
Fix async task being destroyed before cancelled (#2787)
This commit is contained in:
parent
819d72614a
commit
1bea9ea4be
@ -47,14 +47,19 @@ class AsyncIteratorCallbackHandler(AsyncCallbackHandler):
|
|||||||
while not self.queue.empty() or not self.done.is_set():
|
while not self.queue.empty() or not self.done.is_set():
|
||||||
# Wait for the next token in the queue,
|
# Wait for the next token in the queue,
|
||||||
# but stop waiting if the done event is set
|
# but stop waiting if the done event is set
|
||||||
done, _ = await asyncio.wait(
|
done, other = await asyncio.wait(
|
||||||
[
|
[
|
||||||
|
# NOTE: If you add other tasks here, update the code below,
|
||||||
|
# which assumes each set has exactly one task each
|
||||||
asyncio.ensure_future(self.queue.get()),
|
asyncio.ensure_future(self.queue.get()),
|
||||||
asyncio.ensure_future(self.done.wait()),
|
asyncio.ensure_future(self.done.wait()),
|
||||||
],
|
],
|
||||||
return_when=asyncio.FIRST_COMPLETED,
|
return_when=asyncio.FIRST_COMPLETED,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Cancel the other task
|
||||||
|
other.pop().cancel()
|
||||||
|
|
||||||
# Extract the value of the first completed task
|
# Extract the value of the first completed task
|
||||||
token_or_done = cast(Union[str, Literal[True]], done.pop().result())
|
token_or_done = cast(Union[str, Literal[True]], done.pop().result())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user