Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法让程序暂停一段时间,但仍有另一段代码在运行?_Python_Time - Fatal编程技术网

Python 有没有办法让程序暂停一段时间,但仍有另一段代码在运行?

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

我想要一个for循环在发生这种情况时运行,我该如何做到这一点

您可以使用任务的并发执行来实现您想要做的事情, 看一看代码,它可以帮助你清楚地理解并发编程

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()