在 Python 内重复一项任务
Repeat a task in Python
我正在使用 Python 开发一个简单的 shell 程序,我需要每秒重复一个任务(一个简单的 os.system
调用)。
是否可以在不中断程序流程的情况下做到这一点?
像多线程的东西?
提前致谢。
没有穿线
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()
我正在使用 Python 开发一个简单的 shell 程序,我需要每秒重复一个任务(一个简单的 os.system
调用)。
是否可以在不中断程序流程的情况下做到这一点? 像多线程的东西?
提前致谢。
没有穿线
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()