@smarisov666

Как отменить task asyncio в потоке?

Имею такую ошибку:
task started
task started
True
True
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\fazuq\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py", line 654, in sleep
    return await future
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\fazuq\Desktop\back in progmramming\telegram-spamer\telegram-spamer-gui\modules\telegram_spamer.py", line 55, in register_session
    start_tasks: tuple = await asyncio.gather(*tasks)
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\fazuq\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner
    self.run()
  File "C:\Users\fazuq\AppData\Local\Programs\Python\Python39\lib\site-packages\threadify\threadify.py", line 192, in run
    task_running = self.task(self.task_storage)
  File "C:\Users\fazuq\Desktop\back in progmramming\telegram-spamer\telegram-spamer-gui\modules\telegram_spamer.py", line 62, in start_thread
    asyncio.run(register_session())
  File "C:\Users\fazuq\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\fazuq\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
asyncio.exceptions.CancelledError

import asyncio
import threading
from modules import settings
from threadify import Threadify


async def message_thread(session_name: str, api_id: int, api_hash: str, proxy: dict, channels: list) -> None:   
    print('task started')
    await asyncio.sleep(5)
    print('task live')

tasks: list[object] = []
async def register_session() -> TelegramClient:
    global tasks
    for account in settings.config['accounts']:
        tasks.append(asyncio.ensure_future(message_thread(account['account_name'], account['api_id'], account['api_hash'], account['proxy'], account['channels'])))
    start_tasks: tuple = await asyncio.gather(*tasks)




def start_thread(storage):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    asyncio.run(register_session())

    
thread = None

def start(status: bool):
    global thread, tasks
    if status:
        thread = Threadify(start_thread)
        thread.start()
    else:
        for task in tasks:
            print(task.cancel())
        thread.kill()
  • Вопрос задан
  • 78 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы