Discuss / Python / python3.11这写法又有新变化了

python3.11这写法又有新变化了

Topic source

#1 Created at ... [Delete] [Delete and Lock User]

python3.11这写法又有新变化了,具体如下:

import threading
import asyncio
async def hello():
    print('Hello world! (%s)' % threading.current_thread())
    await asyncio.sleep(3)
    print('Hello again! (%s)' % threading.current_thread())
async def sayHi():
    print('Hi world! (%s)' % threading.current_thread())
    await asyncio.sleep(1)
    print('Hi again! (%s)' % threading.current_thread())
loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(hello()),asyncio.ensure_future(sayHi())]
loop.run_until_complete(asyncio.gather(*tasks))
loop.close()

#2 Created at ... [Delete] [Delete and Lock User]

运行结果如下:

Hello world! (<_MainThread(MainThread, started 5680)>)
Hi world! (<_MainThread(MainThread, started 5680)>)
Hi again! (<_MainThread(MainThread, started 5680)>)
Hello again! (<_MainThread(MainThread, started 5680)>)
PS F:\Users\hkan\Documents\VisualStudioCode\PythonLearnitagain> 

juven永恒

#3 Created at ... [Delete] [Delete and Lock User]

现在asyncio.get_event_loop()也要deprecated了,我试着换成了asyncio.new_event_loop(),但就开始报错了, 请问有什么好办法吗?


  • 1

Reply