Skip to content

Commit 794cb4b

Browse files
committed
some optimizations for async locks and events
1 parent 86a3497 commit 794cb4b

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

robotcode/jsonrpc2/protocol.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import re
77
import threading
8+
import time
89
import weakref
910
from abc import ABC, abstractmethod
1011
from collections import OrderedDict
@@ -608,8 +609,13 @@ def set_result(f: asyncio.Future[Any], r: Any, ev: threading.Event) -> None:
608609

609610
entry.future.get_loop().call_soon_threadsafe(set_result, entry.future, res, done)
610611

611-
if not done.wait(120):
612-
raise TimeoutError("Can't set response result")
612+
start = time.monotonic()
613+
while not done.is_set():
614+
615+
if time.monotonic() - start > 120:
616+
raise TimeoutError("Can't set future result.")
617+
618+
await asyncio.sleep(0.001)
613619

614620
else:
615621
self._logger.warning("Response loop is not running.")

robotcode/language_server/robotframework/parts/references.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@ async def _find_references_in_workspace(
116116

117117
result: List[Location] = []
118118

119+
# tasks = []
119120
for doc in self.parent.documents.documents:
120121
if doc.language_id == "robotframework":
121122
result.extend(await func(doc, *args, **kwargs))
123+
# tasks.append(run_coroutine_in_thread(func, doc, *args, **kwargs))
124+
125+
# result = await asyncio.gather(*tasks)
122126

123127
return result
124128

robotcode/utils/async_tools.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import functools
88
import inspect
99
import threading
10+
import time
1011
import weakref
1112
from collections import deque
1213
from concurrent.futures.thread import ThreadPoolExecutor
@@ -443,7 +444,7 @@ def set(self) -> None:
443444
fut.set_result(True)
444445
else:
445446

446-
def s(w: asyncio.Future[Any], ev: threading.Event) -> None:
447+
def set_result(w: asyncio.Future[Any], ev: threading.Event) -> None:
447448
try:
448449
if not w.done():
449450
w.set_result(True)
@@ -453,7 +454,7 @@ def s(w: asyncio.Future[Any], ev: threading.Event) -> None:
453454
if not fut.done():
454455
done = threading.Event()
455456

456-
fut.get_loop().call_soon_threadsafe(s, fut, done)
457+
fut.get_loop().call_soon_threadsafe(set_result, fut, done)
457458

458459
if not done.wait(120):
459460
raise TimeoutError("Callback timeout.")
@@ -519,8 +520,13 @@ def set_result(w: asyncio.Future[Any], ev: threading.Event) -> None:
519520

520521
waiter.get_loop().call_soon_threadsafe(set_result, waiter, done)
521522

522-
if not done.wait(120):
523-
raise TimeoutError("Callback timeout.")
523+
start = time.monotonic()
524+
while not done.is_set():
525+
526+
if time.monotonic() - start > 120:
527+
raise TimeoutError("Can't set future result.")
528+
529+
await asyncio.sleep(0.001)
524530

525531
def locked(self) -> bool:
526532
with self._lock:
@@ -708,8 +714,16 @@ def set_result(w: asyncio.Future[Any], ev: threading.Event) -> None:
708714

709715
fut.get_loop().call_soon_threadsafe(set_result, fut, done)
710716

711-
if not done.wait(120):
712-
raise TimeoutError("Callback timeout.")
717+
start = time.monotonic()
718+
while not done.is_set():
719+
720+
if time.monotonic() - start > 120:
721+
raise TimeoutError("Can't set future result.")
722+
723+
await asyncio.sleep(0.001)
724+
725+
# if not done.wait(120):
726+
# raise TimeoutError("Callback timeout.")
713727

714728

715729
class FutureInfo:

0 commit comments

Comments
 (0)