Skip to content

Commit 7eda6a9

Browse files
committed
Introduce thread-safety
1 parent 575b969 commit 7eda6a9

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

vk_api/vk_api.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import re
1212
import time
13+
import threading
1314

1415
import requests
1516

@@ -100,10 +101,12 @@ def __init__(self, login=None, password=None, number=None, sec_number=None,
100101
TWOFACTOR_CODE: auth_handler or self.auth_handler
101102
}
102103

104+
self.lock = threading.Lock()
105+
103106
def authorization(self, reauth=False):
104107
""" Полная авторизация с получением токена
105108
106-
:param reauth: Позволяет переавторизиваться, игнорируя сохраненные
109+
:param reauth: Позволяет переавторизиваться, игнорируя сохраненные
107110
куки и токен
108111
"""
109112

@@ -390,14 +393,15 @@ def method(self, method, values=None, captcha_sid=None, captcha_key=None):
390393
'captcha_key': captcha_key
391394
})
392395

393-
# Ограничение 3 запроса в секунду
394-
delay = DELAY - (time.time() - self.last_request)
396+
with self.lock:
397+
# Ограничение 3 запроса в секунду
398+
delay = DELAY - (time.time() - self.last_request)
395399

396-
if delay > 0:
397-
time.sleep(delay)
400+
if delay > 0:
401+
time.sleep(delay)
398402

399-
response = self.http.post(url, values)
400-
self.last_request = time.time()
403+
response = self.http.post(url, values)
404+
self.last_request = time.time()
401405

402406
if response.ok:
403407
response = response.json()

0 commit comments

Comments
 (0)