{"id":4133,"date":"2022-07-18T04:35:51","date_gmt":"2022-07-18T04:35:51","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4133"},"modified":"2023-06-03T07:34:40","modified_gmt":"2023-06-03T07:34:40","slug":"python-event-loop","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-concurrency\/python-event-loop\/","title":{"rendered":"Python Event Loop"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll 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\" id='introduction-to-the-python-event-loop'>Introduction to the Python event loop <a href=\"#introduction-to-the-python-event-loop\" class=\"anchor\" id=\"introduction-to-the-python-event-loop\" title=\"Anchor for Introduction to the Python event loop\">#<\/a><\/h2>\n\n\n\n<p>Concurrency means multiple <a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-create_task\/\">tasks<\/a> can run at the same time. The <code>asyncio<\/code> built-in package allows you to run tasks concurrently using a single <a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-threading\/\">thread<\/a>. <\/p>\n\n\n\n<p>To achieve a single-threaded concurrency model, the <code>asyncio<\/code> package uses a construct called an event loop. To understand how the event loop works, let&#8217;s take an example of writing data into a file.<\/p>\n\n\n\n<p>When <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-write-text-file\/\">writing data to a file<\/a>, 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 <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-thread\/\">thread<\/a>. 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&#8217;s 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 <code>asyncio<\/code> package uses an event loop. The following picture illustrates how the event loop work:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><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 <code>asyncio<\/code> package provides some functions that allow you to automatically manage the event loop so you don&#8217;t need to deal with low-level API.<\/p>\n\n\n\n<p>In the next tutorial, you&#8217;ll learn how to define coroutines using the <code><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-async-await\/\">async<\/a><\/code> keyword and pause them using the <code>await<\/code> keyword.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concurrency means running multiple tasks at the same time.<\/li>\n\n\n\n<li>The event loop is responsible for getting tasks from an event queue and handling it.<\/li>\n\n\n\n<li>The <code>asyncio<\/code> package uses an event loop to achieve a single-threaded concurrency model.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4133\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-event-loop\/\"\n\t\t\t\tdata-post-title=\"Python Event Loop\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4133\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-event-loop\/\"\n\t\t\t\tdata-post-title=\"Python Event Loop\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Python event loop and how Python uses it to achieve the concurrency model using a single thread.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4104,"menu_order":14,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4133","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=4133"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4133\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4104"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}