我有以下代码
from pyrogram import Client, filters
from settings import *
app = Client("my_account", api_id=api_id, api_hash=api_hash)
groups_id = {-1001694: 20, -1001786: 10} # вместо настоящих айди фейковые
def send(app, id, time):
while True:
app.send_message(id, "test222")
sleep(time)
with app:
for id, time in groups_id.items():
send(app, id, time) # строка запуска
所以这里的问题是:如何多次运行发送,即 在“起跑线”之后,循环继续工作,就好像没有永恒的循环一样,但它实际上会起作用吗?我尝试了异步和并行但没有任何效果
这是异步版本:
from settings import *
import asyncio
app = Client("my_account", api_id=api_id, api_hash=api_hash)
groups_id = {-1001694: 20, -1001786: 10} # вместо настоящих айди фейковые
async def send(app, id, time):
while True:
await app.send_message(id, "test222")
await asyncio.sleep(time)
async def main():
async with app:
for id, time in groups_id.items():
await send(app, id, time) # строка запуска
asyncio.run(main())
但这里的结果是一样的,只发送给一组
更新:感谢此人的回复:insolor
这是最终代码:
from settings import *
import asyncio
app = Client("my_account", api_id=api_id, api_hash=api_hash)
groups_id = {-100169: 20, -1001729: 10} # вместо настоящих айди фейковые
async def send(app, id, time):
while True:
await app.send_message(id, "test222")
await asyncio.sleep(time)
async def main():
async with app:
await asyncio.gather(*[send(app, id, time) for id, time in groups_id.items()])
asyncio.run(main())