简体   繁体   English

Telethon Python asyncio TypeError: 'coroutine' 对象不可调用

[英]Telethon Python asyncio TypeError: 'coroutine' object is not callable

you need to get information about the channel, chat, group.您需要获取有关频道、聊天、群组的信息。 Since each account has a restriction on receiving information, I will use several accounts.由于每个账号都有接收信息的限制,我会用几个账号。 I'm trying to get information, but I get an error:我正在尝试获取信息,但出现错误:

 Traceback (most recent call last):
  File "D:/Git/telegram/telegram_new.py", line 50, in parse_entity
    channel=entity
TypeError: 'coroutine' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Git/telegram/telegram_new.py", line 56, in parse_entity
    id=entity
TypeError: 'coroutine' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Git/telegram/telegram_new.py", line 60, in parse_entity
    chat_id=entity
TypeError: 'coroutine' object is not callable

Help fix the error帮助修复错误

Code:代码:

import logging
from telethon import TelegramClient,sync, utils, errors
from telethon.tl.types import PeerUser, PeerChat
from telethon import functions, types
import pandas as pd
import time
import re
import asyncio
from contextlib import suppress
import traceback

log = logging.getLogger(__name__)
format = '%(asctime)s %(levelname)s:%(message)s'
logging.basicConfig(format=format, level=logging.INFO)

api_id = ""              #(my.telegram.org)
api_hash = ""

filename_excel = "project_for_export_no_formulas_31_10_18.xlsx"
filename_numbers = "number.txt"

queue_entity = asyncio.Queue()
numbers = []
count_thread = 8
def load_numbers(filename):
    with open(filename, "r") as file:
        content = file.read().split("\n")
        for conten in content:
            numbers.append(conten)

def load_excel(output_filename):
    data = pd.read_excel(output_filename, 'Projects', dtype=str)
    for item in data["Telegram link"]:
        if item != "nan":
            queue_entity.put_nowait(item)


async def create_client(number):
    print(number)
    client = TelegramClient(number, api_id, api_hash).start()
    return client

async def parse_entity(entity, client_coroutine):
    result = None
    client = await client_coroutine
    try:
        result = client(functions.channels.GetFullChannelRequest(
                channel=entity
            ))
        print("Успешно")
    except TypeError:
        try:
            result = client(functions.users.GetFullUserRequest(
                id=entity
            ))
        except TypeError:
            result = client(functions.messages.GetFullChatRequest(
                chat_id=entity
            ))
    except errors.UsernameInvalidError:
        print("Не найден пользователь, канал или чат")
    except errors.InviteHashExpiredError:
        print("Чата больше нет")
    except errors.InviteHashInvalidError:
        print("Ссылка приглашения не валидна")
    except ValueError:
        print("Невозможно получить entity. Для начала нужно вступить в группу или чат")
    except errors.FloodWaitError:
        print("Ожидание суток")
    return result


async def crawl(future):
    futures = []
    numbers = await future
    for client in asyncio.as_completed([create_client(number) for number in numbers]):
        while queue_entity.qsize() > 0:
            futures.append(asyncio.ensure_future(parse_entity(queue_entity.get_nowait(), client)))
    if futures:
        await asyncio.wait(futures)

async def start_main(root):
    print(root)
    loop = asyncio.get_event_loop()
    initial_future = loop.create_future()
    initial_future.set_result(root)
    await crawl(initial_future)

if __name__ == '__main__':
    start = time.time()
    load_numbers(filename_numbers) #Загрузка телефонов
    load_excel(filename_excel)     #Загрузка Excel
    loop = asyncio.get_event_loop()
    # loop.set_debug(True)
    try:
        loop.run_until_complete(start_main(numbers))
    except KeyboardInterrupt:
        for task in asyncio.Task.all_tasks():
            task.cancel()
            with suppress(asyncio.CancelledError):
                loop.run_until_complete(task)
    finally:
        loop.close()
    log.info("Time work: %s", time.time() - start)

In my regional forum, no one could help me, so I am writing here, since the English community is more, someone can help在我的区域论坛里,没有人可以帮助我,所以我写在这里,因为英语社区比较多,有人可以帮助

Update更新

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Git/telegram/telegram_new.py", line 54, in parse_entity
    result = client(functions.users.GetFullUserRequest(id=entity))
TypeError: 'coroutine' object is not callable

Update 2更新 2

future: <Task finished coro=<parse_entity() done, defined at D:/Git/telegram/telegram_new.py:45> exception=RuntimeError('coroutine is being awaited already')>

Update 3更新 3

Traceback (most recent call last):
  File "D:/Git/telegram/telegram_new.py", line 95, in <module>
    loop.run_until_complete(start_main(numbers))
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 568, in run_until_complete
    return future.result()
  File "D:/Git/telegram/telegram_new.py", line 86, in start_main
    await crawl(initial_future)
  File "D:/Git/telegram/telegram_new.py", line 75, in crawl
    for client in asyncio.as_completed([await create_client(number) for number in numbers]):
  File "C:\ProgramData\Anaconda3\lib\asyncio\tasks.py", line 505, in as_completed
    todo = {ensure_future(f, loop=loop) for f in set(fs)}
  File "C:\ProgramData\Anaconda3\lib\asyncio\tasks.py", line 505, in <setcomp>
    todo = {ensure_future(f, loop=loop) for f in set(fs)}
  File "C:\ProgramData\Anaconda3\lib\asyncio\tasks.py", line 588, in ensure_future
    raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
TypeError: An asyncio.Future, a coroutine or an awaitable is required
2018-12-27 18:14:49,534 ERROR:Task was destroyed but it is pending!
task: <Task pending coro=<UpdateMethods._update_loop() running at C:\ProgramData\Anaconda3\lib\site-packages\telethon\client\updates.py:215> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001A379824108>()]>>
2018-12-27 18:14:49,534 ERROR:Task was destroyed but it is pending!
task: <Task pending coro=<MTProtoSender._send_loop() running at C:\ProgramData\Anaconda3\lib\site-packages\telethon\network\mtprotosender.py:375> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001A379824138>()]>>
2018-12-27 18:14:49,534 ERROR:Task was destroyed but it is pending!
task: <Task pending coro=<MTProtoSender._recv_loop() running at C:\ProgramData\Anaconda3\lib\site-packages\telethon\network\mtprotosender.py:413> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001A3798242B8>()]>>
Exception ignored in: <coroutine object MTProtoSender._recv_loop at 0x000001A37980A948>
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\telethon\network\mtprotosender.py", line 413, in _recv_loop
  File "C:\ProgramData\Anaconda3\lib\site-packages\telethon\network\connection\connection.py", line 123, in recv
  File "C:\ProgramData\Anaconda3\lib\asyncio\queues.py", line 161, in get
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 672, in call_soon
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 461, in _check_closed
RuntimeError: Event loop is closed
2018-12-27 18:14:49,534 ERROR:Task was destroyed but it is pending!
task: <Task pending coro=<Connection._send_loop() running at C:\ProgramData\Anaconda3\lib\site-packages\telethon\network\connection\connection.py:135> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001A379824228>()]>>
2018-12-27 18:14:49,534 ERROR:Task was destroyed but it is pending!
task: <Task pending coro=<Connection._recv_loop() running at C:\ProgramData\Anaconda3\lib\site-packages\telethon\network\connection\connection.py:150> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001A379824288>()]>>
2018-12-27 18:14:49,535 ERROR:Unexpected exception in the send loop
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\asyncio\queues.py", line 159, in get
    await getter
GeneratorExit

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\telethon\network\connection\connection.py", line 135, in _send_loop
    self._send(await self._send_queue.get())
  File "C:\ProgramData\Anaconda3\lib\asyncio\queues.py", line 161, in get
    getter.cancel()  # Just in case getter is not done yet.
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 672, in call_soon
    self._check_closed()
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 461, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <coroutine object Connection._send_loop at 0x000001A37980AA48>
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\telethon\network\connection\connection.py", line 142, in _send_loop
  File "C:\ProgramData\Anaconda3\lib\site-packages\telethon\network\connection\connection.py", line 94, in disconnect
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 672, in call_soon
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 461, in _check_closed
RuntimeError: Event loop is closed

Since TelegramClient.start is an async function you have to await it:由于TelegramClient.start是一个异步函数,您必须await它:

async def create_client(number):
    return await TelegramClient(number, api_id, api_hash).start()

I made two notable changes to your code:我对您的代码进行了两项显着更改:

  1. Wrapping create_client(number) calls in asyncio.as_completed .asyncio.as_completed中包装create_client(number)调用。 The reason is that create_client(number) returns a coroutine object, however asyncio.as_completed expects a list of futures.原因是create_client(number)返回一个协程对象,但是asyncio.as_completed需要一个 futures 列表。 Here is as_completed docstring:这是as_completed文档字符串:

as_completed(fs, *, loop=None, timeout=None) Return an iterator whose values are coroutines. as_completed(fs, *, loop=None, timeout=None) 返回一个值为协程的迭代器。

 When waiting for the yielded coroutines you'll get the results (or exceptions,) of the original Futures (or coroutines). in the order in which and as soon as they complete; This differs from PEP 3148: the proper way to use this is: for f in as_completed(fs). result = await f # The 'await' may raise. # Use result.
  1. Changing loop.close() to loop.stop() .loop.close()更改为loop.stop() Otherwise loop will be closed and we will get exception when there are still running tasks.否则循环将被关闭,当仍有正在运行的任务时我们将得到异常。

Here is how I edited your code to make it work:以下是我如何编辑您的代码以使其工作:

import logging
from telethon import TelegramClient,sync, utils, errors
from telethon.tl.types import PeerUser, PeerChat
from telethon import functions, types
import pandas as pd
import time
import re
import asyncio
from contextlib import suppress
import traceback

log = logging.getLogger(__name__)
format = '%(asctime)s %(levelname)s:%(message)s'
logging.basicConfig(format=format, level=logging.INFO)

api_id = ""
api_hash = ""

filename_excel = "project_for_export_no_formulas_31_10_18.xlsx"
filename_numbers = "number.txt"

queue_entity = asyncio.Queue()
numbers = []
count_thread = 8

def load_numbers(filename):
    with open(filename, "r") as file:
        content = file.read().split("\n")
        for conten in content:
            numbers.append(conten)

def load_excel(output_filename):
    data = pd.read_excel(output_filename, 'Projects', dtype=str)
    for item in data["Telegram link"]:
        if item != "nan":
            queue_entity.put_nowait(item)


async def create_client(number):
    return await TelegramClient(number, api_id, api_hash).start()

async def parse_entity(entity, client):
    result = None
    try:
        result = client(functions.channels.GetFullChannelRequest(
                channel=entity
            ))
        print("Успешно")
    except TypeError:
        try:
            result = client(functions.users.GetFullUserRequest(
                id=entity
            ))
        except TypeError:
            result = client(functions.messages.GetFullChatRequest(
                chat_id=entity
            ))
    except errors.UsernameInvalidError:
        print("Не найден пользователь, канал или чат")
    except errors.InviteHashExpiredError:
        print("Чата больше нет")
    except errors.InviteHashInvalidError:
        print("Ссылка приглашения не валидна")
    except ValueError:
        print("Невозможно получить entity. Для начала нужно вступить в группу или чат")
    except errors.FloodWaitError:
        print("Ожидание суток")
    return result


async def crawl(future):
    futures = []
    numbers = await future
    for f in asyncio.as_completed([asyncio.ensure_future(create_client(number)) for number in numbers]):
        client = await f
        while queue_entity.qsize() > 0:
            futures.append(asyncio.ensure_future(parse_entity(queue_entity.get_nowait(), client)))
    if futures:
        await asyncio.wait(futures)

async def start_main(root):
    loop = asyncio.get_event_loop()
    initial_future = loop.create_future()
    initial_future.set_result(root)
    await crawl(initial_future)

if __name__ == '__main__':
    start = time.time()
    load_numbers(filename_numbers) #Загрузка телефонов
    #load_excel(filename_excel)     #Загрузка Excel
    loop = asyncio.get_event_loop()
    # loop.set_debug(True)
    try:
        loop.run_until_complete(start_main(numbers))
    except KeyboardInterrupt:
        for task in asyncio.Task.all_tasks():
            task.cancel()
            with suppress(asyncio.CancelledError):
                loop.run_until_complete(task)
    finally:
        loop.stop()
    log.info("Time work: %s", time.time() - start)

I hope it helps.我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM