How to use async/await in Python 3.5?

后端 未结 2 2046
太阳男子
太阳男子 2020-12-24 05:16
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time

async def foo():
  await time.sleep(1)

foo()

I couldn\'t make this dead simple example

2条回答
  •  抹茶落季
    2020-12-24 05:34

    If you already have a loop running (with some other tasks), you can add new tasks with:

    asyncio.ensure_future(foo())
    

    otherwise you might get

    The event loop is already running
    

    error.

提交回复
热议问题