Python 3.5: "asynchronous with" result in SyntaxError. What for?

I am using Python 3.5, which according to PEP 492 should have access to async with syntax, but I get SyntaxError when I try to use it. What am I doing wrong?

 In [14]: sys.version Out[14]: '3.5.2 (default, Oct 11 2016, 04:59:56) \n[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)]' In [15]: async with aiohttp.ClientSession() as session: File "<ipython-input-15-9799c5ce74cf>", line 1 async with aiohttp.ClientSession() as session: ^ SyntaxError: invalid syntax 
+6
source share
1 answer

You cannot use async with without the async function. As the docs say :

This is a SyntaxError for using async with an external async def function.

But this code will work:

 async def some_function(): async with aiohttp.ClientSession() as session: pass 

Or look at an example from docs .

+13
source

Source: https://habr.com/ru/post/1015046/


All Articles