Prevent enabling full_sync multiple times and add method to stop the thread

This commit is contained in:
Tulir Asokan 2018-10-02 02:43:18 +03:00
parent 1189788c2c
commit 362e5b01e2

View File

@ -88,7 +88,14 @@ def _syncify(*types, loop, thread_name):
_syncify_gen(t, method_name, loop, thread_name) _syncify_gen(t, method_name, loop, thread_name)
__asyncthread = None
def enable(loop=None, thread_name="__telethon_async_thread__"): def enable(loop=None, thread_name="__telethon_async_thread__"):
global __asyncthread
if __asyncthread is not None:
raise ValueError("full_sync has already been enabled.")
if not loop: if not loop:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
old_init = TelegramClient.__init__ old_init = TelegramClient.__init__
@ -136,6 +143,14 @@ def enable(loop=None, thread_name="__telethon_async_thread__"):
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
loop.run_forever() loop.run_forever()
asyncthread = threading.Thread(target=start, name=thread_name) __asyncthread = threading.Thread(target=start, name=thread_name)
asyncthread.start() __asyncthread.start()
return asyncthread __asyncthread.loop = loop
return __asyncthread
def stop():
global __asyncthread
if not __asyncthread:
raise ValueError("Can't find asyncio thread")
__asyncthread.loop.call_soon_threadsafe(__asyncthread.loop.stop)