Further improve the session-switching mechanism for inline bots (#739)
* Ability to run multiple bot. Global session creates a problem for other bot. As that session was generated for another bot can't be used by this bot. * Use the existing media session lock Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com>
This commit is contained in:
parent
aea1ffc46f
commit
e68da74e89
@ -16,29 +16,22 @@
|
|||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from asyncio import Lock
|
|
||||||
|
|
||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram import raw
|
from pyrogram import raw
|
||||||
from pyrogram.errors import AuthBytesInvalid
|
from pyrogram.errors import AuthBytesInvalid
|
||||||
from pyrogram.session import Session
|
from pyrogram.session import Session
|
||||||
from pyrogram.session.auth import Auth
|
from pyrogram.session.auth import Auth
|
||||||
|
|
||||||
lock = Lock()
|
|
||||||
sessions = {}
|
|
||||||
|
|
||||||
|
|
||||||
async def get_session(client: "pyrogram.Client", dc_id: int):
|
async def get_session(client: "pyrogram.Client", dc_id: int):
|
||||||
if dc_id == await client.storage.dc_id():
|
if dc_id == await client.storage.dc_id():
|
||||||
return client
|
return client
|
||||||
|
|
||||||
async with lock:
|
async with client.media_sessions_lock:
|
||||||
global sessions
|
if client.media_sessions.get(dc_id):
|
||||||
|
return client.media_sessions[dc_id]
|
||||||
|
|
||||||
if sessions.get(dc_id):
|
session = client.media_sessions[dc_id] = Session(
|
||||||
return sessions[dc_id]
|
|
||||||
|
|
||||||
session = sessions[dc_id] = Session(
|
|
||||||
client, dc_id,
|
client, dc_id,
|
||||||
await Auth(client, dc_id, False).create(),
|
await Auth(client, dc_id, False).create(),
|
||||||
False, is_media=True
|
False, is_media=True
|
||||||
|
Loading…
Reference in New Issue
Block a user