Difference between revisions of "Asyncio python"

From Teknologisk videncenter
Jump to: navigation, search
m
m (Links)
Line 25: Line 25:
 
*[https://realpython.com/async-io-python/ Async IO in Python]
 
*[https://realpython.com/async-io-python/ Async IO in Python]
 
*[https://realpython.com/python-gil/ The Python GIL - Global Interpreter Lock]
 
*[https://realpython.com/python-gil/ The Python GIL - Global Interpreter Lock]
*[https://realpython.com/python-concurrency/ Speed Up Your Python Program With Concurrency]]
+
*[https://realpython.com/python-concurrency/ Speed Up Your Python Program With Concurrency]
  
 
[[Category:Python]]
 
[[Category:Python]]

Revision as of 06:24, 23 February 2025

For a primer see Yield python

await vs. asyncio.create()

import asyncio
import time

async def fetch_data(url):
    # Simulate a long-running network request
    await asyncio.sleep(2)  # Wait for 2 seconds
    print(f"Data fetched from {url}")

async def main():
    task1 = asyncio.create_task(fetch_data("https://example.com"))
    task2 = asyncio.create_task(fetch_data("https://another.example.com"))

    await task1
    await task2

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

Links