2 回答
TA贡献1812条经验 获得超5个赞
您可以通过多线程来完成此任务。您让函数与您的时间一起运行,并且函数运行完毕后两者都会立即终止:
import threading
import time
def calc(): #this function generates the square of all numbers between 1 and 56000000
for i in range(1,56000000):
i*i
t1 = threading.Thread(target=calc)
t1.start() #starting a thread with the calc function
i = 1
while t1.is_alive(): #Check if the thread is alive
time.sleep(1)# print time after every second
print(f'Time elapsed ----------- {i}s')
i = i+1
t1.join() #terminate thread once calc function is done
print('Done!')
TA贡献1856条经验 获得超5个赞
您永远不应该使用time.sleepwithdiscord.py因为它会停止您应该使用的整个机器人await asyncio.sleep(1)。
您也可以创建此命令。
import datetime as dt
bot.launch_time = dt.datetime.utcnow()
@bot.command()
async def uptime(ctx):
delta_uptime = dt.datetime.utcnow() - bot.launch_time
hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
days, hours = divmod(hours, 24)
await ctx.send(f"{days}d, {hours}h, {minutes}m, {seconds}s")
现在您可以使用{prefix}uptime它来了解它已经运行了多长时间。
添加回答
举报