Skip to content

Create 协程,多进程多线程 - #1

Open
Gloria668866 wants to merge 1 commit into
mainfrom
python的学习相关的知识点

Hidden character warning

The head ref may contain hidden characters: "python\u7684\u5b66\u4e60\u76f8\u5173\u7684\u77e5\u8bc6\u70b9"
Open

Create 协程,多进程多线程#1
Gloria668866 wants to merge 1 commit into
mainfrom
python的学习相关的知识点

Conversation

@Gloria668866

Copy link
Copy Markdown
Owner

# import time
# import asyncio #异步操作模块
#
#
#
# def func1():
#     print("func1")
#     time.sleep(2)
#     print("你好,我是帅哥1")
#
#
# def func2():
#     print("func2")
#     time.sleep(3)
#     print("你好,我是帅哥2")
# def func3():
#     print("func3")
#     time.sleep(4)
#     print("你好,我是帅哥3")
#
# def func4():
#     print("func4")
#     time.sleep(5)
#     print("你好,我是帅哥4")

#还未异步的时间是14s

import time
import asyncio #异步操作模块

async def func1():
    print("func1")
    await asyncio.sleep(2)
    # time.sleep(2)
    print("你好,我是帅哥1")


async def func2():
    print("func2")
    # time.sleep(3)
    await asyncio.sleep(3)#异步睡眠代替同步睡眠
    print("你好,我是帅哥2")
async def func3():
    print("func3")
    await asyncio.sleep(4)
    # time.sleep(4)
    print("你好,我是帅哥3")

async def func4():
    print("func4")
    #
    await asyncio.sleep(5)
    print("你好,我是帅哥4")
async def main():
    await asyncio.gather(func1(), func2(), func3(), func4())

if __name__ == '__main__':
    t2 = time.time()
    # f1=func1()
    # f2=func2()
    # f3=func3()
    # f4=func4()
    asyncio.run(main())
    t1=time.time()
    t=t1-t2
    print("异步运行的时间",t)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant