Python3.6 AttributeError: module 'asyncio' has no attribute 'run'

前端 未结 2 1851
小蘑菇
小蘑菇 2020-12-08 09:38

I tried to read https://hackernoon.com/asynchronous-python-45df84b82434. It\'s about asynchronous python and I tried the code from this, but I\'m getting a weird Error. The

相关标签:
2条回答
  • 2020-12-08 09:56

    asyncio.run is a Python 3.7 addition. In 3.5-3.6, your example is roughly equivalent to:

    import asyncio
    
    futures = [...]
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait(futures))
    
    0 讨论(0)
  • 2020-12-08 10:10

    The asyncio.run() function was added in Python 3.7. From the asyncio.run() function documentation:

    New in version 3.7: Important: this function has been added to asyncio in Python 3.7 on a provisional basis.

    Note the provisional part; the Python maintainers forsee that the function may need further tweaking and updating, so the API may change in future Python versions.

    At any rate, you can't use it on Python 3.6. You'll have to upgrade or implement your own.

    A very simple approximation would be to use loop.run_until_complete():

    loop = asyncio.get_event_loop()
    result = loop.run_until_complete(coro)
    

    although this ignores handling remaining tasks that may still be running. See the asyncio.runners source code for the complete asyncio.run() implementation.

    0 讨论(0)
提交回复
热议问题