Untitled

mail@pastecode.io avatar
unknown
python
a year ago
752 B
5
Indexable
import asyncio
import contextvars
import resource  # For memory usage tracking on Unix systems

# Define a context variable
my_context_var = contextvars.ContextVar('my_context_var')

async def set_context_var():
    my_context_var.set(1)
    await asyncio.sleep(0.01)  # Simulate some work

async def main():
    # Track memory usage before and after running the program
    start_memory = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

    for _ in range(10000):
        await set_context_var()
    
    end_memory = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

    print(f"Start Memory Usage: {start_memory} KB")
    print(f"End Memory Usage: {end_memory} KB")


if __name__ == '__main__':
    asyncio.run(main())