我有一个带有图表的机器人。一种检查时间是否已用完的功能。这是代码:
async def check_timestamp():
while True:
try:
with open("list.json", "r") as file:
data = json.load(file)
current_time = int(time.time())
for username, (timestamp, userid) in list(data["time"].items()):
if current_time >= timestamp:
user = bot.get_user(int(userid))
await user.send("This is training time!")
del data["time"][username]
time.sleep(1)
with open("list.json", "w") as file:
json.dump(data, file, indent=4)
del data
except Exception as e:
print(e)
time.sleep(1)
@bot.event
async def on_ready():
print("Hello, world!")
asyncio.create_task(check_timestamp())
如果删除create_task(),那么一切都会正常。我的错误是什么?
time.sleep()阻止整个过程,但asyncio.sleep()仅阻止特定功能。循环的每次迭代可能都会出现异常,因此进程几乎一直处于休眠状态。尝试调用print(e, flush=true)- 这将强制 python 在下一个操作之前将数据“推送”到输出流中。很可能是因为time.sleep您没有看到异常