Python 有没有办法让程序暂停一段时间,但仍有另一段代码在运行?
我想要一个for循环在发生这种情况时运行,我该如何做到这一点您可以使用任务的并发执行来实现您想要做的事情, 看一看代码,它可以帮助你清楚地理解并发编程Python 有没有办法让程序暂停一段时间,但仍有另一段代码在运行?,python,time,Python,Time,我想要一个for循环在发生这种情况时运行,我该如何做到这一点您可以使用任务的并发执行来实现您想要做的事情, 看一看代码,它可以帮助你清楚地理解并发编程 import time time.sleep(1) 使用一个在调用Sleep之前启动的线程,你能给我一个例子吗?另请参见asyncio。我不能升级,因为我没有足够的声誉 import time from concurrent import futures def busy(): print("\nRunning in seprate
import time
time.sleep(1)
使用一个在调用Sleep之前启动的线程,你能给我一个例子吗?另请参见asyncio。我不能升级,因为我没有足够的声誉
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()