鱼C论坛

 找回密码
 立即注册
查看: 3061|回复: 2

[已解决]async 同步计时问题。。。

[复制链接]
发表于 2023-5-8 16:51:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1.             async def my_event_handler(event):
  2.                 global t_result
  3.                 global stop_condition
  4.                 stop_condition = True
  5.                 ply = event.text
  6.                 t_result={as:ply}
  7.                 await client.disconnect()
  8.                 return stop_condition,t_result                    

  9.             async def timer():
  10.                 # 计时器代码
  11.                 global t_result
  12.                 global stop_condition
  13.                 time.sleep(5)
  14.                 stop_condition = False
  15.                 print("timer Exception")
  16.                 client.disconnect()
  17.                 t_result={as:"超时,请稍后重试!"}
  18.                 return stop_condition,t_result


  19.             with client:
  20.                 client.run_until_disconnected()
  21.             return stop_condition,t_result
复制代码


想加个async 同步计时, 超过5秒程序就直接返回超时,但现在加了async def timer(): 没效果,请大佬帮看下,感谢!
最佳答案
2023-5-8 17:55:03
根据你提供的代码,`timer()`函数似乎并没有被调用。为了使用异步计时器,你可以使用`asyncio.sleep()`函数。这里是一个可能会起作用的修改版本:


  1. import asyncio

  2. async def my_event_handler(event):
  3.     global t_result
  4.     global stop_condition
  5.     stop_condition = True
  6.     ply = event.text
  7.     t_result={as:ply}
  8.     await client.disconnect()
  9.     return stop_condition, t_result                    

  10. async def timer():
  11.     # 计时器代码
  12.     global t_result
  13.     global stop_condition
  14.     await asyncio.sleep(5)  # 使用asyncio.sleep()实现计时
  15.     stop_condition = False
  16.     print("timer Exception")
  17.     await client.disconnect()
  18.     t_result={as:"超时,请稍后重试!"}
  19.     return stop_condition,t_result


  20. with client:
  21.     asyncio.ensure_future(timer())  # 启动计时器的运行
  22.     client.run_until_disconnected()

  23. return stop_condition,t_result
复制代码


在上面的代码中,将计时器函数与事件处理程序串联起来,然后通过`asyncio.ensure_future()`函数启动计时器。这应该会在事件处理程序与计时器之间创建一个协程链,使其同时运行,并且在事件处理程序返回结果或者计时器完成时结束。
有用请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-5-8 16:52:19 | 显示全部楼层
你可以使用 asyncio.sleep() 方法来实现异步计时器。下面是修改后的代码:

  1. import asyncio

  2. async def my_event_handler(event):
  3.     global t_result
  4.     global stop_condition
  5.     stop_condition = True
  6.     ply = event.text
  7.     t_result = {as:ply}
  8.     await client.disconnect()
  9.     return stop_condition, t_result                    

  10. async def timer():
  11.     # 计时器代码
  12.     global t_result
  13.     global stop_condition
  14.     await asyncio.sleep(5)
  15.     stop_condition = False
  16.     print("timer Exception")
  17.     await client.disconnect()
  18.     t_result = {as:"超时,请稍后重试!"}
  19.     return stop_condition, t_result

  20. loop = asyncio.get_event_loop()
  21. tasks = [loop.create_task(my_event_handler(event)), loop.create_task(timer())]
  22. done, pending = loop.run_until_complete(asyncio.wait(tasks, timeout=5))
  23. if len(done) == 0:
  24.     # 超时处理
  25.     stop_condition = False
  26.     t_result = {as:"超时,请稍后重试!"}
  27. else:
  28.     # 处理结果
  29.     for task in done:
  30.         stop_condition, t_result = task.result()

  31. loop.close()
  32. return stop_condition, t_result
复制代码


这里使用 asyncio.wait() 方法来同时等待事件处理和计时器任务的完成,设置超时时间为 5 秒。如果在超时时间内没有完成任务,则返回超时提示。如果完成了任务,则返回任务的结果。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-8 17:55:03 | 显示全部楼层    本楼为最佳答案   
根据你提供的代码,`timer()`函数似乎并没有被调用。为了使用异步计时器,你可以使用`asyncio.sleep()`函数。这里是一个可能会起作用的修改版本:


  1. import asyncio

  2. async def my_event_handler(event):
  3.     global t_result
  4.     global stop_condition
  5.     stop_condition = True
  6.     ply = event.text
  7.     t_result={as:ply}
  8.     await client.disconnect()
  9.     return stop_condition, t_result                    

  10. async def timer():
  11.     # 计时器代码
  12.     global t_result
  13.     global stop_condition
  14.     await asyncio.sleep(5)  # 使用asyncio.sleep()实现计时
  15.     stop_condition = False
  16.     print("timer Exception")
  17.     await client.disconnect()
  18.     t_result={as:"超时,请稍后重试!"}
  19.     return stop_condition,t_result


  20. with client:
  21.     asyncio.ensure_future(timer())  # 启动计时器的运行
  22.     client.run_until_disconnected()

  23. return stop_condition,t_result
复制代码


在上面的代码中,将计时器函数与事件处理程序串联起来,然后通过`asyncio.ensure_future()`函数启动计时器。这应该会在事件处理程序与计时器之间创建一个协程链,使其同时运行,并且在事件处理程序返回结果或者计时器完成时结束。
有用请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 03:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表