Skip to content

Commit

Permalink
Merge pull request #16 from yungwine/main
Browse files Browse the repository at this point in the history
fixed .close() method for TonLibClient
  • Loading branch information
psylopunk authored Nov 7, 2022
2 parents 468e441 + 2b12659 commit 371e862
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions ton/tonlibjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def __init__(self, loop, ls_index, cdll_path=None, verbosity_level=0):
self._state = None # None, "finished", "crashed", "stuck"

self.is_dead = False

# creating tasks
self.read_results_task = self.loop.create_task(self.read_results())
self.del_expired_futures_task = self.loop.create_task(self.del_expired_futures_loop())
Expand Down Expand Up @@ -161,7 +160,6 @@ def _execute(self, query, timeout=10):

future_result = self.loop.create_future()
self.futures[extra_id] = future_result

self.loop.run_in_executor(None, lambda: self.send(query))
return future_result

Expand All @@ -181,11 +179,16 @@ async def execute(self, query, timeout=10):
def _is_working(self):
return self._state not in ('crashed', 'stuck', 'finished')

@property
def _is_closing(self):
return self._state == 'finishing'

async def close(self):
try:
self._state = 'finishing'
await self.read_results_task
await self.del_expired_futures_task
self._state = 'finished'
await self.read_results_task()
await self.del_expired_futures_task()
except Exception as ee:
logger.error(f"Exception in tonlibjson.close: {traceback.format_exc()}")
raise RuntimeError(f'Error in tonlibjson.close: {ee}')
Expand All @@ -207,7 +210,7 @@ async def read_results(self):
delta = 5
receive_func = functools.partial(self.receive, timeout)
try:
while self._is_working:
while self._is_working and not self._is_closing:
# return reading result
result = None
try:
Expand Down Expand Up @@ -235,10 +238,10 @@ async def read_results(self):

async def del_expired_futures_loop(self):
try:
while self._is_working:
while self._is_working and not self._is_closing:
self.cancel_futures()
await asyncio.sleep(1)

self.cancel_futures(cancel_all=True)
except Exception as ee:
logger.critical(f'Task del_expired_futures_loop failed: {ee}')
logger.critical(f'Task del_expired_futures_loop failed: {ee}')

0 comments on commit 371e862

Please sign in to comment.