{"id":1992,"date":"2024-06-27T10:23:42","date_gmt":"2024-06-27T10:23:42","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=1992"},"modified":"2024-06-27T10:23:49","modified_gmt":"2024-06-27T10:23:49","slug":"deep-dive-into-pythons-asyncio-for-concurrency","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/","title":{"rendered":"Deep Dive into Python&#8217;s asyncio for Concurrency"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Concurrency is a powerful concept that can dramatically increase the performance of your applications. Python&#8217;s <code>asyncio<\/code> module is a great tool for implementing concurrency in your code. This tutorial will provide an in-depth look at <code>asyncio<\/code>, aimed at developers who are already comfortable with Python and want to deepen their understanding of asynchronous programming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Introduction to Concurrency<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Concurrency involves executing multiple tasks simultaneously, which can significantly improve the efficiency of programs that perform I\/O-bound operations. Unlike parallelism, which involves running multiple tasks at the same time across multiple processors, concurrency involves interleaving tasks on a single processor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use Concurrency?<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Improved Performance:<\/strong> Especially for I\/O-bound tasks like web scraping, network requests, and file I\/O.<\/li>\n\n\n\n<li><strong>Responsiveness:<\/strong> Keeps your applications responsive by handling multiple tasks seemingly at once.<\/li>\n\n\n\n<li><strong>Efficient Resource Utilization:<\/strong> Makes better use of CPU and I\/O resources.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. Understanding <code>asyncio<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>asyncio<\/code> is a library to write concurrent code using the async\/await syntax. It provides a foundation for asynchronous programming by allowing you to execute tasks concurrently, which can be particularly beneficial for I\/O-bound tasks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Features of <code>asyncio<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Event Loop:<\/strong> The core of every <code>asyncio<\/code> application. It runs asynchronous tasks and callbacks, performs network I\/O operations, and manages subprocesses.<\/li>\n\n\n\n<li><strong>Coroutines:<\/strong> Special functions that can pause their execution and allow other coroutines to run.<\/li>\n\n\n\n<li><strong>Futures and Tasks:<\/strong> Objects that represent the result of an asynchronous computation.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. Key Concepts in <code>asyncio<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To effectively use <code>asyncio<\/code>, you need to understand its core concepts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Coroutines<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Coroutines are the building blocks of <code>asyncio<\/code> applications. They are similar to regular functions but can be paused and resumed, allowing other coroutines to run in the meantime.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> asyncio\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    print(<span class=\"hljs-string\">\"Hello\"<\/span>)\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n    print(<span class=\"hljs-string\">\"World\"<\/span>)\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Event Loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The event loop is the central component of <code>asyncio<\/code> applications. It runs the coroutines and handles all asynchronous operations.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">loop = asyncio.get_event_loop()\nloop.run_until_complete(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Tasks<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Tasks are used to schedule coroutines concurrently. They are a higher-level construct that runs coroutines in the event loop.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">say_hello<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n    print(<span class=\"hljs-string\">\"Hello\"<\/span>)\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">say_world<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n    print(<span class=\"hljs-string\">\"World\"<\/span>)\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    task1 = asyncio.create_task(say_hello())\n    task2 = asyncio.create_task(say_world())\n    <span class=\"hljs-keyword\">await<\/span> task1\n    <span class=\"hljs-keyword\">await<\/span> task2\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Futures<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Futures represent the result of an asynchronous computation that may not be completed yet.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_after<\/span><span class=\"hljs-params\">(future, delay, value)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(delay)\n    future.set_result(value)\n\nloop = asyncio.get_event_loop()\nfuture = asyncio.Future()\nloop.run_until_complete(set_after(future, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'Hello'<\/span>))\nprint(future.result())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">4. Basic <code>asyncio<\/code> Usage<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Running a Simple Coroutine<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To run a coroutine, use <code>asyncio.run()<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">hello_world<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    print(<span class=\"hljs-string\">\"Hello, World!\"<\/span>)\n\nasyncio.run(hello_world())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Awaiting on Coroutines<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the <code>await<\/code> keyword to pause a coroutine and wait for another coroutine to finish.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    print(<span class=\"hljs-string\">\"Starting...\"<\/span>)\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">2<\/span>)\n    print(<span class=\"hljs-string\">\"Finished!\"<\/span>)\n\nasyncio.run(greet())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Scheduling Coroutines with <code>asyncio.create_task<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can schedule multiple coroutines to run concurrently using <code>asyncio.create_task<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">say_hello<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n    print(<span class=\"hljs-string\">\"Hello\"<\/span>)\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">say_goodbye<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">2<\/span>)\n    print(<span class=\"hljs-string\">\"Goodbye\"<\/span>)\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    task1 = asyncio.create_task(say_hello())\n    task2 = asyncio.create_task(say_goodbye())\n    <span class=\"hljs-keyword\">await<\/span> task1\n    <span class=\"hljs-keyword\">await<\/span> task2\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">5. Advanced <code>asyncio<\/code> Patterns<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Creating and Running Coroutines<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can create coroutines using the <code>async def<\/code> syntax and run them using <code>await<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">compute_square<\/span><span class=\"hljs-params\">(x)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n    <span class=\"hljs-keyword\">return<\/span> x * x\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    result = <span class=\"hljs-keyword\">await<\/span> compute_square(<span class=\"hljs-number\">5<\/span>)\n    print(result)\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Managing Multiple Coroutines<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can run multiple coroutines concurrently using <code>asyncio.gather<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">task1<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Task 1 result'<\/span>\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">task2<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">2<\/span>)\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Task 2 result'<\/span>\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    results = <span class=\"hljs-keyword\">await<\/span> asyncio.gather(task1(), task2())\n    print(results)\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Synchronization Primitives<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>asyncio<\/code> provides several synchronization primitives like locks, events, semaphores, and queues.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">lock = asyncio.Lock()\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">worker<\/span><span class=\"hljs-params\">(name, lock)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-keyword\">with<\/span> lock:\n        print(<span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{name}<\/span> acquired the lock\"<\/span>)\n        <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n        print(<span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{name}<\/span> released the lock\"<\/span>)\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.gather(worker(<span class=\"hljs-string\">'worker1'<\/span>, lock), worker(<span class=\"hljs-string\">'worker2'<\/span>, lock))\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">6. Error Handling in <code>asyncio<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Error handling in <code>asyncio<\/code> involves managing exceptions within coroutines and tasks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Catching Exceptions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can catch exceptions within coroutines using try\/except blocks.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">faulty_task<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n        <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">\"An error occurred!\"<\/span>)\n    <span class=\"hljs-keyword\">except<\/span> ValueError <span class=\"hljs-keyword\">as<\/span> e:\n        print(<span class=\"hljs-string\">f\"Caught an exception: <span class=\"hljs-subst\">{e}<\/span>\"<\/span>)\n\nasyncio.run(faulty_task())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Handling Task Exceptions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You should handle exceptions in tasks to prevent them from propagating and crashing the program.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">error_task<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n    <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">\"Error in task\"<\/span>)\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    task = asyncio.create_task(error_task())\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-keyword\">await<\/span> task\n    <span class=\"hljs-keyword\">except<\/span> ValueError <span class=\"hljs-keyword\">as<\/span> e:\n        print(<span class=\"hljs-string\">f\"Caught task exception: <span class=\"hljs-subst\">{e}<\/span>\"<\/span>)\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">7. <code>asyncio<\/code> and I\/O-bound Tasks<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>asyncio<\/code> is particularly well-suited for I\/O-bound tasks such as network communication, file I\/O, and database operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Fetching URLs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example of using <code>asyncio<\/code> for fetching multiple URLs concurrently.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> aiohttp\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fetch<\/span><span class=\"hljs-params\">(session, url)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-keyword\">with<\/span> session.get(url) <span class=\"hljs-keyword\">as<\/span> response:\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">await<\/span> response.text()\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    urls = &#91;\n        <span class=\"hljs-string\">'https:\/\/www.example.com'<\/span>,\n        <span class=\"hljs-string\">'https:\/\/www.python.org'<\/span>,\n        <span class=\"hljs-string\">'https:\/\/www.asyncio.org'<\/span>\n    ]\n    <span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-keyword\">with<\/span> aiohttp.ClientSession() <span class=\"hljs-keyword\">as<\/span> session:\n        tasks = &#91;fetch(session, url) <span class=\"hljs-keyword\">for<\/span> url <span class=\"hljs-keyword\">in<\/span> urls]\n        results = <span class=\"hljs-keyword\">await<\/span> asyncio.gather(*tasks)\n        <span class=\"hljs-keyword\">for<\/span> result <span class=\"hljs-keyword\">in<\/span> results:\n            print(result)\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">8. Real-World Examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Web Scraping<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create a simple web scraper using <code>asyncio<\/code> and <code>aiohttp<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> aiohttp\n<span class=\"hljs-keyword\">from<\/span> bs4 <span class=\"hljs-keyword\">import<\/span> BeautifulSoup\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fetch<\/span><span class=\"hljs-params\">(session, url)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-keyword\">with<\/span> session.get(url) <span class=\"hljs-keyword\">as<\/span> response:\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">await<\/span> response.text()\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">scrape<\/span><span class=\"hljs-params\">(url)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-keyword\">with<\/span> aiohttp.ClientSession() <span class=\"hljs-keyword\">as<\/span> session:\n        html = <span class=\"hljs-keyword\">await<\/span> fetch(session, url)\n        soup = BeautifulSoup(html, <span class=\"hljs-string\">'html.parser'<\/span>)\n        <span class=\"hljs-keyword\">return<\/span> soup.title.string\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    urls = &#91;\n        <span class=\"hljs-string\">'https:\/\/www.example.com'<\/span>,\n        <span class=\"hljs-string\">'https:\/\/www.python.org'<\/span>,\n        <span class=\"hljs-string\">'https:\/\/www.asyncio.org'<\/span>\n    ]\n    tasks = &#91;scrape(url) <span class=\"hljs-keyword\">for<\/span> url <span class=\"hljs-keyword\">in<\/span> urls]\n    results = <span class=\"hljs-keyword\">await<\/span> asyncio.gather(*tasks)\n    <span class=\"hljs-keyword\">for<\/span> result <span class=\"hljs-keyword\">in<\/span> results:\n        print(result)\n\nasyncio.run(main())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Example: Database Operations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Using <code>aiomysql<\/code> to perform asynchronous database operations.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> aiomysql\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">query_database<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    conn = <span class=\"hljs-keyword\">await<\/span> aiomysql.connect(\n        host=<span class=\"hljs-string\">'localhost'<\/span>, port=<span class=\"hljs-number\">3306<\/span>,\n        user=<span class=\"hljs-string\">'root'<\/span>, password=<span class=\"hljs-string\">'password'<\/span>,\n        db=<span class=\"hljs-string\">'test'<\/span>\n    )\n\n    <span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-keyword\">with<\/span> conn.cursor() <span class=\"hljs-keyword\">as<\/span> cursor:\n        <span class=\"hljs-keyword\">await<\/span> cursor.execute(<span class=\"hljs-string\">\"SELECT * FROM users;\"<\/span>)\n        result = <span class=\"hljs-keyword\">await<\/span> cursor.fetchall()\n        print(result)\n    conn.close()\n\nasyncio.run(query_database())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">9. Performance Considerations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">While <code>asyncio<\/code> can improve the performance of I\/O-bound tasks, it&#8217;s important to consider the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Task Overhead:<\/strong> Creating and managing a large number of tasks can add overhead.<\/li>\n\n\n\n<li><strong>CPU-bound Tasks:<\/strong> For CPU-bound tasks, consider using multiprocessing or other parallelism techniques.<\/li>\n\n\n\n<li><strong>Resource Limits:<\/strong> Be mindful of the limits of your system&#8217;s resources (e.g., file descriptors, network sockets).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">10. Debugging <code>asyncio<\/code> Code<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Debugging <code>asyncio<\/code> code can be challenging. Here are some tips:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Enable Debug Mode:<\/strong> Use <code>asyncio.run(main(), debug=True)<\/code> to enable debug mode.<\/li>\n\n\n\n<li><strong>Logging:<\/strong> Use the <code>logging<\/code> module to log events and exceptions.<\/li>\n\n\n\n<li><strong>Inspect Tasks:<\/strong> Use <code>asyncio.all_tasks()<\/code> to inspect running tasks.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Debugging with Logging<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> logging\n\nlogging.basicConfig(level=logging.DEBUG)\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">faulty_task<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">await<\/span> asyncio.sleep(<span class=\"hljs-number\">1<\/span>)\n    <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">\"An error occurred!\"<\/span>)\n\n<span class=\"hljs-keyword\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    task = asyncio.create_task(faulty_task())\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-keyword\">await<\/span> task\n    <span class=\"hljs-keyword\">except<\/span> ValueError <span class=\"hljs-keyword\">as<\/span> e:\n        logging.error(<span class=\"hljs-string\">f\"Caught an exception: <span class=\"hljs-subst\">{e}<\/span>\"<\/span>)\n\nasyncio.run(main(), debug=<span class=\"hljs-literal\">True<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">11. Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Concurrency is a powerful tool for improving the performance and responsiveness of your applications, particularly for I\/O-bound tasks. Python&#8217;s <code>asyncio<\/code> module provides a robust framework for implementing asynchronous programming using the async\/await syntax. By understanding the key concepts and patterns in <code>asyncio<\/code>, you can write efficient and effective concurrent code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Concurrency is a powerful concept that can dramatically increase the performance of your applications. Python&#8217;s asyncio module is a great tool for implementing concurrency in your code. This tutorial will provide an in-depth look at asyncio, aimed at developers who are already comfortable with Python and want to deepen their understanding of asynchronous programming. 1. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-1992","post","type-post","status-publish","format-standard","category-programming-languages","category-python","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Deep Dive into Python&#039;s asyncio for Concurrency<\/title>\n<meta name=\"description\" content=\"Concurrency is a powerful concept that can dramatically increase the performance of your applications. Python&#039;s asyncio module is a great tool\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deep Dive into Python&#039;s asyncio for Concurrency\" \/>\n<meta property=\"og:description\" content=\"Concurrency is a powerful concept that can dramatically increase the performance of your applications. Python&#039;s asyncio module is a great tool\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-27T10:23:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-27T10:23:49+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/deep-dive-into-pythons-asyncio-for-concurrency\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/deep-dive-into-pythons-asyncio-for-concurrency\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"Deep Dive into Python&#8217;s asyncio for Concurrency\",\"datePublished\":\"2024-06-27T10:23:42+00:00\",\"dateModified\":\"2024-06-27T10:23:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/deep-dive-into-pythons-asyncio-for-concurrency\\\/\"},\"wordCount\":664,\"articleSection\":[\"Programming Languages\",\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/deep-dive-into-pythons-asyncio-for-concurrency\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/deep-dive-into-pythons-asyncio-for-concurrency\\\/\",\"name\":\"Deep Dive into Python's asyncio for Concurrency\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2024-06-27T10:23:42+00:00\",\"dateModified\":\"2024-06-27T10:23:49+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"Concurrency is a powerful concept that can dramatically increase the performance of your applications. Python's asyncio module is a great tool\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/deep-dive-into-pythons-asyncio-for-concurrency\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/deep-dive-into-pythons-asyncio-for-concurrency\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/deep-dive-into-pythons-asyncio-for-concurrency\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Deep Dive into Python&#8217;s asyncio for Concurrency\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Deep Dive into Python's asyncio for Concurrency","description":"Concurrency is a powerful concept that can dramatically increase the performance of your applications. Python's asyncio module is a great tool","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/","og_locale":"en_US","og_type":"article","og_title":"Deep Dive into Python's asyncio for Concurrency","og_description":"Concurrency is a powerful concept that can dramatically increase the performance of your applications. Python's asyncio module is a great tool","og_url":"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/","article_published_time":"2024-06-27T10:23:42+00:00","article_modified_time":"2024-06-27T10:23:49+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"Deep Dive into Python&#8217;s asyncio for Concurrency","datePublished":"2024-06-27T10:23:42+00:00","dateModified":"2024-06-27T10:23:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/"},"wordCount":664,"articleSection":["Programming Languages","Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/","url":"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/","name":"Deep Dive into Python's asyncio for Concurrency","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2024-06-27T10:23:42+00:00","dateModified":"2024-06-27T10:23:49+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"Concurrency is a powerful concept that can dramatically increase the performance of your applications. Python's asyncio module is a great tool","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/deep-dive-into-pythons-asyncio-for-concurrency\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"Python","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/python\/"},{"@type":"ListItem","position":4,"name":"Deep Dive into Python&#8217;s asyncio for Concurrency"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=1992"}],"version-history":[{"count":1,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1992\/revisions"}],"predecessor-version":[{"id":1993,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1992\/revisions\/1993"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=1992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=1992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=1992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}