I am using asyncio for an application in a very basic way. Checking most tutorials around the internet (and even the official docs), I see that they use the get_event_loop() and loop.run_until_complete():
import asyncio async def say(what, when): await asyncio.sleep(when) print(what) loop = asyncio.get_event_loop() loop.run_until_complete(say('hello world', 1)) loop.close() But in the Python 3.7 docs, we can read:
Application developers should typically use the high-level asyncio functions, such as asyncio.run(), and should rarely need to reference the loop object or call its methods. This section is intended mostly for authors of lower-level code, libraries, and frameworks, who need finer control over the event loop behavior.
I found it much cleaner and easier to use, but it only works for Python 3.7+. So here I would have to make a choice, whether to use Python 3.7+ and run() or make it compatible with Python 3.6 and use the event loop. How would you manage this? Is there a simple way to make it backwards compatible with Python 3.6? Should I check Python version first and use either one way or another based on that, until Python 3.7 becomes a common version?
asyncio.runon older Python versions is not hard, and you get the advantage that you test your code under the conditions set up byasyncio.run, i.e. on a freshly created event loop.