如何在后台延迟运行任务?

时间:2019-12-19 16:02:53

标签: python time sleep

我有以下代码:

import time


def wait10seconds():
    for i in range(10):
        time.sleep(1)

    return 'Counted to 10!'


print(wait10seconds())

print('test')

现在我的问题是如何在执行功能print('test')之前在不交换两行的情况下使wait10seconds()运行。

我希望输出如下:

test
Counted to 10!

有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

您可以为此使用//*[@id='site-wrapper']//a[contains(text(), 'Report')] <a target="_blank" href="/Bentley/Reports/74/0/5323914"> <span class="fa fa-print"></span> Report </a>

喜欢:

Threads

答案 1 :(得分:-1)

如果您使用的是python 3.5+,则可以使用asyncio:

import asyncio
async def wait10seconds():
    for i in range(10):
        await asyncio.sleep(1)
    return 'Counted to 10!'

print(asyncio.run(wait10seconds()))

asyncio.run是python 3.7的新功能,对于python 3.5和3.6,您将无法使用asyncio.run,但可以通过直接使用event_loop来实现相同的目的