2 回答

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可以通過(guò)多線程來(lái)完成此任務(wù)。您讓函數(shù)與您的時(shí)間一起運(yùn)行,并且函數(shù)運(yùn)行完畢后兩者都會(huì)立即終止:
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貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊
您永遠(yuǎn)不應(yīng)該使用time.sleepwithdiscord.py因?yàn)樗鼤?huì)停止您應(yīng)該使用的整個(gè)機(jī)器人await asyncio.sleep(1)。
您也可以創(chuàng)建此命令。
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")
現(xiàn)在您可以使用{prefix}uptime它來(lái)了解它已經(jīng)運(yùn)行了多長(zhǎng)時(shí)間。
添加回答
舉報(bào)