Set the main event loop in threads where there's no event loop

#480
This commit is contained in:
Dan 2020-08-31 00:17:33 +02:00
parent be62ac365f
commit 617f0c9b30
2 changed files with 13 additions and 0 deletions

View File

@ -33,6 +33,11 @@ class ContinuePropagation(StopAsyncIteration):
pass
import asyncio
from . import raw, types, filters, handlers, emoji
from .client import Client
from .sync import idle
# Save the main thread loop for future references
main_event_loop = asyncio.get_event_loop()

View File

@ -23,6 +23,7 @@ import re
import sys
from pathlib import Path
import pyrogram
from pyrogram import __version__
from pyrogram.parser import Parser
from pyrogram.session.internals import MsgId
@ -73,6 +74,13 @@ class Scaffold:
mime_types_to_extensions[mime_type] = " ".join(extensions)
def __init__(self):
try:
asyncio.get_event_loop()
except RuntimeError:
# This happens when creating Client instances inside different threads that don't have an event loop.
# Set the main event loop in this thread.
asyncio.set_event_loop(pyrogram.main_event_loop)
self.session_name = None
self.api_id = None
self.api_hash = None