【发布时间】:2021-07-16 13:12:32
【问题描述】:
我正在使用python3,我想同时执行多个任务,而无需等待另一个。我想在python中使用异步编程来实现它(不使用多处理或线程)。
注意:我尝试了 python asyncio 库,但无法得到我想要的。
import time
def func1():
print("This is the beginning of function 1.")
func2()
print("This is the end of function 1.")
def func2():
time.sleep(5)
print("This is Function 2.")
func1()
我有两个函数 func1() 和 func2()。我想在 func1() 中调用 func2() 函数,我不想让 func1() 等待 func2() 完成。
预期结果:
>> This is the beginning of function 1.
>> This is the end of function 1.
>> After 5 seconds: This is Function 2.
我需要详细的代码答案。提前谢谢你。
【问题讨论】:
-
Stack Overflow 无意取代现有的教程或文档。除此之外,与您的想法相反,它不是免费的编码服务。您应该发送honest attempt at the solution,然后然后仅在遇到问题时询问具体问题。
-
@martineau 我认为这个问题没有错。我搜索了解决方案,但找不到,这就是我在这里发布它的原因。我不知道你为什么对这个问题投了反对票。
-
有很多关于这样做的教程,包括 Python documentation 中的示例——这显然不是您搜索工作的一部分……此外,如果您尝试了某些操作,请将代码中的代码作为 @ 987654323@.
标签: python python-3.x asynchronous python-asyncio