python如何手动实现阻塞

 Q小泓别_431 发布于 2022-10-27 22:02

python 如何手动实现阻塞?

令py脚本一直阻塞直到强制退出(KeyboardInterrupt)

之前用

while True:
    pass

发现cpu占用太高了。。。

2 个回答
  • while True:
        time.sleep(1)
    2022-10-29 05:40 回答
  • 1. 使用time.sleep

    while True:
        # 等待一年
        time.sleep(60*60*24*365)
        
    # 直接等待一百年
    time.sleep(60*60*24*365*100)

    2.使用threading.Condition

    condition=threading.Condition()
    condition.acquire()
    condition.wait()

    2.使用等待键盘响应

    Python实现按任意键继续

    2022-10-29 05:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有