Как заставить две функции работать одновременно?
Есть две функции flud и flud_to_phantom:
async def flud(account....):
for account in account_list:
try:
app = Client(name=account[1:])
async def connect_to_main():
async with app:
await app.join_chat(chat_id=chat_link)
await app.stop()
app.run(connect_to_main())
except UserAlreadyParticipant:
print('акаунт уже в группе')
print('Успешно')
while True:
app_flud = Client(name=account)
text = choice(messages)
async def in_flud():
async with app_flud:
await app_flud.send_message(chat_id=chat.id, text=text)
await app_flud.stop()
app_flud.run(in_flud())
acc_n = counter(acc_n, account_list)
print(f'аккаунт {acc_n}/{len(account_list)} отправил сообщение')
await asyncio.sleep(delay)
async def flud_to_phantom(new...):
if is_phantom:
while True:
app = Client(name=account)
text = choice(messages)
async def ph_flud():
async with app:
await app.send_message(chat_id=new_chat.id, text=text)
await app.stop()
app.run(ph_flud())
await asyncio.sleep(phantom_delay)
Никак не могу понять как заставить их работать одновременно, мучаюсь уже второй день. Последнее что я пробовал это запускать их через отдельную функцию, но ничего не помогает(
async def runner(ne...):
task_1 = asyncio.create_task(flud(acco...))
task_2 = asyncio.create_task(flud_to_phantom(new_c...))
await asyncio.gather(task_1, task_2)
Практически всегда выскакивает вот такая ошибка:
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
File "C:\Users\...\main.py", line 277, in <module>
main()
File "C:\Users\...\main.py", line 265, in main
loop.run_until_complete(runner(new...
File "C:\Users\...\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
return future.result()
File "C:\Users\...\main.py", line 218, in runner
await asyncio.gather(task_1, task_2)
File "C:\Users\...\main.py", line 98, in flud
app.run(connect_to_main())
File "C:\Users\...\venv\lib\site-packages\pyrogram\methods\utilities\run.py", line 85, in run
run(idle())
File "C:\Users\...\Python310\lib\asyncio\base_events.py", line 622, in run_until_complete
self._check_running()
File "C:\Users...\Python310\lib\asyncio\base_events.py", line 582, in _check_running
raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
sys:1: RuntimeWarning: coroutine 'idle' was never awaited
у меня больше не осталось идеи, что может мне помочь.
Ответы (1 шт):
Автор решения: Mango
→ Ссылка
модуль threading поможет
import threading
threading.Thread(target=flud).start() #паралельная функция
flud_to_phantom() #главная функция
а если хотите добавить агрументы в функцию то после target=flud вставьте код , args=((аргумент1, аргумент 2),)