不知道如何解决电报机器人python中偶数循环的问题
from telethon import TelegramClient, functions, sync, events
from Message import fetch_text, fetch_list
import time
from telethon.errors import *
from threading import *
import asyncio
class Shill():
def __init__(self,T_id,T_hash,owner):
self.t_id = T_id
self.t_hash = T_hash
self.owner = owner
self.channel_list = fetch_list()
self.message = fetch_text()
def connection(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
我认为,Thast Probles Somwehere在这里,但是不能正确定义
self.client = TelegramClient(self.owner, self.t_id, self.t_hash)
self.client.start()
print("Успешно залогинен")
def join_channel(self):
for var in self.channel_list:
try:
result = self.client(functions.channels.JoinChannelRequest(channel=var))
print(f"{self.owner}. зашел в {var} ")
time.sleep(1)
except Exception as ex:
print(ex)
time.sleep(1)
continue
print("Вошли во все каналы")
def send_message(self):
while True:
for var in self.channel_list:
try:
entity = self.client.get_entity(var)
self.client.send_message(entity, self.message)
print(f"{self.owner}. отправлено")
print("________________________________________")
time.sleep(10)
except Exception as ex:
print(ex)
time.sleep(600)
continue
time.sleep(3600)
def account(self):
self.connection()
self.join_channel()
#self.send_message()
id0 = Shill("","","Me") # Telegram account info
t1 = Thread(target=id0.account)
t1.start()
t1.join()
这种异步问题是什么问题,而像\\任务这样的追溯却被销毁了,但它正在待处理! 任务:<任务未决名称='task-3'coro =< connection._send_loop()
任务被摧毁了,但正在待处理! 任务:<任务待处理名称='task-4'coro =< connection._recv_loop() 任务被摧毁了,但正在待处理! 任务:<任务未决名称='task-5'coro =< mtprotosender._send_loop() 任务被摧毁了,但正在待处理! 任务:<任务待处理名称='task-6'coro =< mtprotosender._recv_loop()运行 任务被摧毁了,但正在待处理! 任务:<任务待处理名称='task-7'coro =< updateMethods._update_loop()
from telethon import TelegramClient, functions, sync, events
from Message import fetch_text, fetch_list
import time
from telethon.errors import *
from threading import *
import asyncio
class Shill():
def __init__(self,T_id,T_hash,owner):
self.t_id = T_id
self.t_hash = T_hash
self.owner = owner
self.channel_list = fetch_list()
self.message = fetch_text()
def connection(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
I think, thast probles somwehere here, but cant define properly
self.client = TelegramClient(self.owner, self.t_id, self.t_hash)
self.client.start()
print("Успешно залогинен")
def join_channel(self):
for var in self.channel_list:
try:
result = self.client(functions.channels.JoinChannelRequest(channel=var))
print(f"{self.owner}. зашел в {var} ")
time.sleep(1)
except Exception as ex:
print(ex)
time.sleep(1)
continue
print("Вошли во все каналы")
def send_message(self):
while True:
for var in self.channel_list:
try:
entity = self.client.get_entity(var)
self.client.send_message(entity, self.message)
print(f"{self.owner}. отправлено")
print("________________________________________")
time.sleep(10)
except Exception as ex:
print(ex)
time.sleep(600)
continue
time.sleep(3600)
def account(self):
self.connection()
self.join_channel()
#self.send_message()
id0 = Shill("","","Me") # Telegram account info
t1 = Thread(target=id0.account)
t1.start()
t1.join()
Have no idea whats wrong with this asyncio and a tracebacks like \\Task was destroyed but it is pending!
task: <Task pending name='Task-3' coro=<Connection._send_loop()
Task was destroyed but it is pending!
task: <Task pending name='Task-4' coro=<Connection._recv_loop()
Task was destroyed but it is pending!
task: <Task pending name='Task-5' coro=<MTProtoSender._send_loop()
Task was destroyed but it is pending!
task: <Task pending name='Task-6' coro=<MTProtoSender._recv_loop() running
Task was destroyed but it is pending!
task: <Task pending name='Task-7' coro=<UpdateMethods._update_loop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近与Asyncio合作,并且有类似的问题。当我键入键盘(CTRL+C)脚本时,我的出现了。
我看到您正在使用异步函数做某事,如果强制关闭异步循环,则可能仍在等待运行或完成运行的任务。该追溯告诉您,在事件循环中,您有5个任务。您是否在Shill类中调用任何异步功能?
当您要停止脚本时,必须确保异步功能完成,并停止并关闭该事件循环。
I was recently working with asyncio, and had similar problem. Mine showed up when I was KeyboardInterupting (ctrl+c) the script.
I see you are doing something with async functions, if you force close the asyncio loop, you might have tasks that are still waiting to run or finish running. That traceback is telling you that you have 5 tasks pending in the event loop. Do you in your Shill class call any async function?
When you want to stop the script you have to make sure the async functions finish and that event loop is stopped and closed.