Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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异步IO中防止阻塞_Python_Python 3.x_Python Asyncio - Fatal编程技术网

如何在python异步IO中防止阻塞

如何在python异步IO中防止阻塞,python,python-3.x,python-asyncio,Python,Python 3.x,Python Asyncio,假设以下代码: import asyncio import time async def say_after(): for i in range(10): await asyncio.sleep(1) print("first function") async def say_after2(): for i in range(10): await asyncio.sleep(1) print("second fun

假设以下代码:

import asyncio
import time

async def say_after():
    for i in range(10):
        await asyncio.sleep(1)
        print("first function")
async def say_after2():
    for i in range(10):
        await asyncio.sleep(1)
        print("second function")

async def main():
    await say_after()
    await say_after2()
asyncio.run(main())

它将首先打印
“第一个功能”
10次,在功能
完成后,它将打印
“第二个功能”
10次。现在我想同时运行这两个函数,就像两个线程(类似并行)一样,使用
asycio
而不是顺序执行。如何执行此操作?

此代码运行的Python版本可能存在重复?在Python3.6中,异步IO没有运行属性,它是3.7
.run
是新的。不管您是否知道3.6中的代码可能与运行该代码的Python版本完全相同?在Python3.6中,异步IO没有运行属性,它是3.7
.run
是新的。无论你是否有任何想法在3.6将是好的