-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi_thread_handler.py
40 lines (29 loc) · 1.27 KB
/
multi_thread_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import threading, time, sys
from typing import List
from translate import deep_translate, google_cloud_translate
class MultiThreadHandler:
def __init__(self):
self.MAX_REQUESTS_PER_SECOND = 12
self.request_times = []
self.request_lock = threading.Lock()
self.print_lock = threading.Lock()
def safe_print(self, *args, **kwargs):
with self.print_lock:
print(*args, **kwargs)
sys.stdout.flush()
def rate_limited_translate(self, text: str) -> str:
with self.request_lock:
current_time = time.time()
# Remove old request times
self.request_times = [t for t in self.request_times if current_time - t < 1]
# If we've made too many requests recently, wait
if len(self.request_times) >= self.MAX_REQUESTS_PER_SECOND:
sleep_time = 1
self.safe_print(f"Sleeping for {sleep_time} seconds/s")
time.sleep(sleep_time)
self.request_times.append(time.time())
self.safe_print(f"Sent a request on {round(time.time(), 3)}")
return deep_translate(text)
def sdk_translate(self, text: List[str]) -> List[str]:
return google_cloud_translate(text)
mth = MultiThreadHandler()