Skip to content

Commit

Permalink
Fixup for previous commit. Lock implementation in ThreadedHistory was…
Browse files Browse the repository at this point in the history
… not correct.
  • Loading branch information
jonathanslenders committed Jan 22, 2021
1 parent 58fa073 commit 99092a8
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions prompt_toolkit/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import os
import threading
from abc import ABCMeta, abstractmethod
from typing import AsyncGenerator, Iterable, List, Optional, Sequence
from typing import AsyncGenerator, Iterable, List, Optional, Sequence, Tuple

__all__ = [
"History",
Expand Down Expand Up @@ -158,18 +158,14 @@ async def load(self) -> AsyncGenerator[str, None]:
continue

# Read new items (in lock).
# (Important: acquiring the lock should happen *in* the try
# block. Otherwise it's not guaranteed it will ever be
# released. This can happen when this coroutine is cancelled at
# an "await" point. This did actually happen when continuously
# pasting huge amounts of text in ptpython.)
try:
await loop.run_in_executor(None, self._lock.acquire)
new_items = self._loaded_strings[items_yielded:]
done = self._loaded
event.clear()
finally:
self._lock.release()
def in_executor() -> Tuple[List[str], bool]:
with self._lock:
new_items = self._loaded_strings[items_yielded:]
done = self._loaded
event.clear()
return new_items, done

new_items, done = await loop.run_in_executor(None, in_executor)

items_yielded += len(new_items)

Expand Down

0 comments on commit 99092a8

Please sign in to comment.