import time
time.sleep(1)
我想要一个 for 循环在发生这种情况时运行,我该怎么做
最佳答案
你可以使用任务的并发执行来实现你想要做的事情, 查看代码可以帮助您清楚地了解并发编程,
import time
from concurrent import futures
def busy():
print("\nRunning in seprate thread")
print("\nTask started")
time.sleep(5)
print("\nTask accomplished")
def main():
executor = futures.ThreadPoolExecutor()
executor.submit(busy)
for i in range(100):
print("\nRunning in main thread")
time.sleep(1)
executor.shutdown() # --> wait for all the tasks to get finished
main()
关于python - 有没有办法让一个程序暂停一段时间,但仍然有另一段代码在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60370162/