{"id":321,"date":"2024-01-28T10:50:14","date_gmt":"2024-01-28T10:50:14","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=321"},"modified":"2024-01-28T10:50:15","modified_gmt":"2024-01-28T10:50:15","slug":"python-event-loop","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/28\/python-event-loop\/","title":{"rendered":"Python Event Loop"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python event loop and how Python uses it to achieve the concurrency model using a single thread.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python event loop<\/h2>\n\n\n\n<p>Concurrency means multiple\u00a0tasks\u00a0can run at the same time. The\u00a0<code>asyncio<\/code>\u00a0built-in package allows you to run tasks concurrently using a single\u00a0thread.<\/p>\n\n\n\n<p>To achieve a single-threaded concurrency model, the&nbsp;<code>asyncio<\/code>&nbsp;package uses a construct called an event loop. To understand how the event loop works, let\u2019s take an example of writing data into a file.<\/p>\n\n\n\n<p>When\u00a0writing data to a file, you perform the following steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the file<\/li>\n\n\n\n<li>Write data to a file and wait for it to be completed<\/li>\n\n\n\n<li>Close the file<\/li>\n<\/ul>\n\n\n\n<p>In this flow, the second task is blocking. Internally, it works like this:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the function, which writes data to the file, sends data to the operating system (OS).<\/li>\n\n\n\n<li>Second, the OS takes over and starts writing data to the file.<\/li>\n\n\n\n<li>Third, the OS notifies the program once it completes writing the file.<\/li>\n<\/ul>\n\n\n\n<p>To manage the notifications, the different OS uses different event notification systems. For example:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>OS<\/th><th>Event Notification System<\/th><\/tr><\/thead><tbody><tr><td>Linux<\/td><td>epoll<\/td><\/tr><tr><td>Windows<\/td><td>I\/O completion port (IOCP)<\/td><\/tr><tr><td>macOS<\/td><td>kqueue<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>These event notification systems allow us to achieve concurrency using a single\u00a0thread. While the program waits for the OS to notify the completion, it can run other code.<\/p>\n\n\n\n<p>In the single-threaded concurrency model, we have only one thread executing Python code at any time. When we encounter an I\/O bound task, we hand it over to the operating system\u2019s event notification system and run other code.<\/p>\n\n\n\n<p>When the I\/O bound task completes, we can resume the task that was waiting for the result and execute the code that follows the I\/O bound task.<\/p>\n\n\n\n<p>To keep track of I\/O-bound tasks that are waiting for results, the&nbsp;<code>asyncio<\/code>&nbsp;package uses an event loop. The following picture illustrates how the event loop work:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/07\/python-event-loop.svg\" alt=\"\" class=\"wp-image-4134\"\/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the main thread submits tasks to a task queue.<\/li>\n\n\n\n<li>Second, the event loop constantly monitors the task queue and runs the task until it counters I\/O tasks. In this case, the event loop pauses the task and hands it over to the OS.<\/li>\n\n\n\n<li>Third, check for the completed IO tasks. If the task is completed, the OS will notify the program. The event loop then runs the unpaused tasks.<\/li>\n<\/ul>\n\n\n\n<p>These steps are repeated until the task queue is empty.<\/p>\n\n\n\n<p>Prior to Python 3.7, you need to create an event loop and run tasks manually. Fortunately, after Python 3.7, the&nbsp;<code>asyncio<\/code>&nbsp;package provides some functions that allow you to automatically manage the event loop so you don\u2019t need to deal with low-level API.<\/p>\n\n\n\n<p>In the next tutorial, you\u2019ll learn how to define coroutines using the\u00a0<code>async<\/code>\u00a0keyword and pause them using the\u00a0<code>await<\/code>\u00a0keyword.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python event loop and how Python uses it to achieve the concurrency model using a single thread. Introduction to the Python event loop Concurrency means multiple\u00a0tasks\u00a0can run at the same time. The\u00a0asyncio\u00a0built-in package allows you to run tasks concurrently using a single\u00a0thread. To achieve a single-threaded concurrency [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-321","post","type-post","status-publish","format-standard","hentry","category-1-python-concurrency"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=321"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/321\/revisions"}],"predecessor-version":[{"id":322,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/321\/revisions\/322"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}