LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.77k stars 1.39k forks source link

use telethon in thread #1240

Closed mengyyy closed 5 years ago

mengyyy commented 5 years ago

Checklist

Code that causes the issue i read this try to use telethon in thread because sqlite3 could not use in multi process. But i found the example could not run.

https://arabic-telethon.readthedocs.io/en/stable/extra/advanced-usage/mastering-asyncio.html#can-i-use-threads

Version info


import asyncio
import stackprinter
from telethon.sync import TelegramClient
import threading

API_ID = XXXXX
API_HASH = "XXXXXXXXXXXXXXXXXXXX"

def go():
    try:
        loop = asyncio.new_event_loop()
        client = TelegramClient("yuante", API_ID, API_HASH, loop=loop)
        client.send_message("me", "client session name ")
    except:
        print(stackprinter.format())

threading.Thread(target=go).start()

Traceback

>>> File <stdin>, line 5, in go

File /usr/local/lib/python3.7/dist-packages/telethon/sync.py, line 35, in syncified
    32   @functools.wraps(method)
    33   def syncified(*args, **kwargs):
    34       coro = method(*args, **kwargs)
--> 35       loop = asyncio.get_event_loop()
    36       if loop.is_running():
    ..................................................
     functools.wraps = <function 'wraps' functools.py:67>
     method = <function 'MessageMethods.send_message' messages.py:516>
     args = (<telethon.client.telegramclient.TelegramClient object at 0x
             7fb4c5ddaa90>, 'me', 'client session name is yuante', )
     kwargs = {}
     coro = <coroutine object MessageMethods.send_message at 0x7fb4c5e1c
             448>
    ..................................................

File /usr/lib/python3.7/asyncio/events.py, line 644, in get_event_loop
    632  def get_event_loop(self):
 (...)
    640          self.set_event_loop(self.new_event_loop())
    641
    642      if self._local._loop is None:
    643          raise RuntimeError('There is no current event loop in thread %r.'
--> 644                             % threading.current_thread().name)
    645
    ..................................................
     self = <asyncio.unix_events._UnixDefaultEventLoopPolicy object at 0
             x7fb4c5dda940>
     self.set_event_loop = <method '_UnixDefaultEventLoopPolicy.set_event_loop' of <asy
                            ncio.unix_events._UnixDefaultEventLoopPolicy object at 0x7fb
                            4c5dda940> unix_events.py:1105>
     self.new_event_loop = <method 'BaseDefaultEventLoopPolicy.new_event_loop' of <asyn
                            cio.unix_events._UnixDefaultEventLoopPolicy object at 0x7fb4
                            c5dda940> events.py:654>
     self._local._loop = None
     threading.current_thread = <function 'current_thread' threading.py:1214>
    ..................................................

RuntimeError: There is no current event loop in thread 'Thread-1'.
__main__:7: RuntimeWarning: coroutine 'MessageMethods.send_message' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Lonami commented 5 years ago

from telethon.sync uses a different loop and cannot be used the way the documentation states. Not a bug, and you probably don't want to use threads anyway.