【问题标题】:Repeat a task in Python在 Python 中重复一个任务
【发布时间】:2021-04-20 08:51:10
【问题描述】:

我正在使用 Python 编写一个简单的 shell 程序,我需要每秒重复一个任务(一个简单的 os.system 调用)。

是否可以在不中断程序流程的情况下做到这一点? 像多线程的东西?

提前致谢。

【问题讨论】:

  • 我投票结束这个问题,因为没有研究。

标签: python operating-system task python-multithreading


【解决方案1】:

无线程

import time

while True:
   time.sleep(1)
   do_stuff()

带线程

import threading 
import time

def my_func():
  while True:
    time.sleep(1)
    do_stuff()

t1 = threading.Thread(target=my_func)  

t1.start()

【讨论】:

  • 也许do_stuff 应该在自己的线程中启动。
猜你喜欢
  • 2015-02-08
  • 1970-01-01
  • 2016-01-27
  • 1970-01-01
  • 2018-02-12
  • 2011-09-08
  • 1970-01-01
  • 2016-09-28
相关资源
最近更新 更多