Python contextvars asyncio support














































Python contextvars asyncio support



asyncio support



Extended form of asyncio is Asynchronous I/O. It is a library to write concurrent code using
the async/await syntax. Asyncio is used as a foundation for multiple Python asynchronous frameworks that give high-performance network and web-servers, data set association libraries, distributed task queues and so on.

Example code for asyncio

 import asyncio

async def main():

   
print('Welcome to')

   
await asyncio.sleep(1)

   
print('Cppsecrets.com')

asyncio.run(main())
Output of the code above:
Welcome to
Cppsecrets.com
Process finished with exit code 0

Context variables are natively supported in asyncio and are ready to be used without any extra configuration. Below is the code for a simple echo server, that uses a context variable to make the address of a remote client available in the Task that handles that client.

import asyncio

import contextvars

client_addr_var = contextvars.ContextVar(
'client_addr')

def render_goodbye():

# The address of the
currently handled client can be accessed without passing it explicitly to this
function.

   
client_addr = client_addr_var.get()

   
return f'Good bye, client @ {client_addr}\n'.encode()

async def handle_request(reader, writer):

    addr =
writer.transport.get_extra_info(
'socket').getpeername()

    client_addr_var.set(addr)

# In any code that we
call is now possible to get client's address by calling
'client_addr_var.get()'.

   
while True:

        line =
await reader.readline()

       
print(line)

       
if not line.strip():

           
break

       
writer.write(line)



    writer.write(render_goodbye())

    writer.close()

async def main():

    srv =
await asyncio.start_server(

        handle_request,
'127.0.0.1', 8081)



   
async with srv:

       
await srv.serve_forever()

asyncio.run(main())

# To test it you can
use telnet:

# telnet 127.0.0.1 8081


<< PREV


Comments