mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 21:07:59 +00:00
Add ability to choose the amount of worker threads for the main session
This commit is contained in:
parent
e542c73966
commit
7234edad5d
@ -105,7 +105,8 @@ class Client:
|
|||||||
phone_code: str or callable = None,
|
phone_code: str or callable = None,
|
||||||
password: str = None,
|
password: str = None,
|
||||||
first_name: str = None,
|
first_name: str = None,
|
||||||
last_name: str = None):
|
last_name: str = None,
|
||||||
|
workers: int = 4):
|
||||||
self.session_name = session_name
|
self.session_name = session_name
|
||||||
self.test_mode = test_mode
|
self.test_mode = test_mode
|
||||||
|
|
||||||
@ -115,6 +116,8 @@ class Client:
|
|||||||
self.first_name = first_name
|
self.first_name = first_name
|
||||||
self.last_name = last_name
|
self.last_name = last_name
|
||||||
|
|
||||||
|
self.workers = workers
|
||||||
|
|
||||||
self.dc_id = None
|
self.dc_id = None
|
||||||
self.auth_key = None
|
self.auth_key = None
|
||||||
self.user_id = None
|
self.user_id = None
|
||||||
@ -144,7 +147,14 @@ class Client:
|
|||||||
self.load_config()
|
self.load_config()
|
||||||
self.load_session(self.session_name)
|
self.load_session(self.session_name)
|
||||||
|
|
||||||
self.session = Session(self.dc_id, self.test_mode, self.proxy, self.auth_key, self.config.api_id)
|
self.session = Session(
|
||||||
|
self.dc_id,
|
||||||
|
self.test_mode,
|
||||||
|
self.proxy,
|
||||||
|
self.auth_key,
|
||||||
|
self.config.api_id,
|
||||||
|
workers=self.workers
|
||||||
|
)
|
||||||
|
|
||||||
terms = self.session.start()
|
terms = self.session.start()
|
||||||
|
|
||||||
@ -243,7 +253,14 @@ class Client:
|
|||||||
self.dc_id = e.x
|
self.dc_id = e.x
|
||||||
self.auth_key = Auth(self.dc_id, self.test_mode, self.proxy).create()
|
self.auth_key = Auth(self.dc_id, self.test_mode, self.proxy).create()
|
||||||
|
|
||||||
self.session = Session(self.dc_id, self.test_mode, self.proxy, self.auth_key, self.config.api_id)
|
self.session = Session(
|
||||||
|
self.dc_id,
|
||||||
|
self.test_mode,
|
||||||
|
self.proxy,
|
||||||
|
self.auth_key,
|
||||||
|
self.config.api_id,
|
||||||
|
workers=self.workers
|
||||||
|
)
|
||||||
self.session.start()
|
self.session.start()
|
||||||
|
|
||||||
r = self.send(
|
r = self.send(
|
||||||
|
@ -60,7 +60,6 @@ class Session:
|
|||||||
|
|
||||||
INITIAL_SALT = 0x616e67656c696361
|
INITIAL_SALT = 0x616e67656c696361
|
||||||
|
|
||||||
WORKERS = 4
|
|
||||||
WAIT_TIMEOUT = 10
|
WAIT_TIMEOUT = 10
|
||||||
MAX_RETRIES = 5
|
MAX_RETRIES = 5
|
||||||
ACKS_THRESHOLD = 8
|
ACKS_THRESHOLD = 8
|
||||||
@ -68,13 +67,21 @@ class Session:
|
|||||||
|
|
||||||
notice_displayed = False
|
notice_displayed = False
|
||||||
|
|
||||||
def __init__(self, dc_id: int, test_mode: bool, proxy: type, auth_key: bytes, api_id: str, is_cdn: bool = False):
|
def __init__(self,
|
||||||
|
dc_id: int,
|
||||||
|
test_mode: bool,
|
||||||
|
proxy: type,
|
||||||
|
auth_key: bytes,
|
||||||
|
api_id: str,
|
||||||
|
is_cdn: bool = False,
|
||||||
|
workers: int = 2):
|
||||||
if not Session.notice_displayed:
|
if not Session.notice_displayed:
|
||||||
print("Pyrogram v{}, {}".format(__version__, __copyright__))
|
print("Pyrogram v{}, {}".format(__version__, __copyright__))
|
||||||
print("Licensed under the terms of the " + __license__, end="\n\n")
|
print("Licensed under the terms of the " + __license__, end="\n\n")
|
||||||
Session.notice_displayed = True
|
Session.notice_displayed = True
|
||||||
|
|
||||||
self.is_cdn = is_cdn
|
self.is_cdn = is_cdn
|
||||||
|
self.workers = workers
|
||||||
|
|
||||||
self.connection = Connection(DataCenter(dc_id, test_mode), proxy)
|
self.connection = Connection(DataCenter(dc_id, test_mode), proxy)
|
||||||
|
|
||||||
@ -115,7 +122,7 @@ class Session:
|
|||||||
try:
|
try:
|
||||||
self.connection.connect()
|
self.connection.connect()
|
||||||
|
|
||||||
for i in range(self.WORKERS):
|
for i in range(self.workers):
|
||||||
Thread(target=self.worker, name="Worker#{}".format(i + 1)).start()
|
Thread(target=self.worker, name="Worker#{}".format(i + 1)).start()
|
||||||
|
|
||||||
Thread(target=self.recv, name="RecvThread").start()
|
Thread(target=self.recv, name="RecvThread").start()
|
||||||
@ -175,7 +182,7 @@ class Session:
|
|||||||
|
|
||||||
self.connection.close()
|
self.connection.close()
|
||||||
|
|
||||||
for i in range(self.WORKERS):
|
for i in range(self.workers):
|
||||||
self.recv_queue.put(None)
|
self.recv_queue.put(None)
|
||||||
|
|
||||||
log.debug("Session stopped")
|
log.debug("Session stopped")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user