Home  >  Q&A  >  body text

python如何手动实现阻塞

python 如何手动实现阻塞?

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

之前用

while True:
    pass

发现cpu占用太高了。。。

伊谢尔伦伊谢尔伦2741 days ago550

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-18 10:33:04

    1. Use time.sleep

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

    2.Use threading.Condition

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

    2. Use wait for keyboard response

    Python implements pressing any key to continue

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:33:04

    while True:
        time.sleep(1)

    reply
    0
  • Cancelreply