{"id":4104,"date":"2022-07-15T03:33:27","date_gmt":"2022-07-15T03:33:27","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4104"},"modified":"2023-08-21T06:45:00","modified_gmt":"2023-08-21T06:45:00","slug":"python-concurrency","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-concurrency\/","title":{"rendered":"Python Concurrency"},"content":{"rendered":"\n<p>In this section, you&#8217;ll learn about Python concurrency including multithreading, multiprocessing, and asynchronous programming from scratch.<\/p>\n\n\n\n<p>What you&#8217;ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Build high-performance &amp; responsive Python applications using concurrency techniques.<\/li>\n\n\n\n<li>Develop multithreaded applications using multithreading.<\/li>\n\n\n\n<li>Develop a program that processes tasks in parallel.<\/li>\n\n\n\n<li>Understand the single-threaded concurrency model.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='multithreading'>Section 1. Multithreading <a href=\"#multithreading\" class=\"anchor\" id=\"multithreading\" title=\"Anchor for Section 1. Multithreading\">#<\/a><\/h2>\n\n\n\n<p>In this section, you&#8217;ll have a good understanding of processes and threads and how to develop multithreaded programs.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/differences-between-processes-and-threads\/\">Understanding Processes and Threads<\/a> &#8211; help you understand the processes and threads, and the main differences between them.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-threading\/\">Threading<\/a> &#8211; show you how to use the threading module to develop a multi-threaded application.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-thread-class\/\">Extending the Thread class<\/a> &#8211; learn how to extend the Thread class to run multiple threads in a program.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-thread-return-value\/\">Returning values from a Thread<\/a> &#8211; show you how to return values from a thread by extending the Thread class.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-multithreading-example\/\">Multithreading Example<\/a> &#8211; build a multithreaded program that scraps stock prices.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-daemon-threads\/\">Daemon threads<\/a> &#8211; learn about daemon threads.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-threadpoolexecutor\/\">Thread Pools<\/a> &#8211; guide you on managing multiple threads efficiently using the thread pool.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='thread-synchronization-techniques'>Section 2. Thread Synchronization Techniques <a href=\"#thread-synchronization-techniques\" class=\"anchor\" id=\"thread-synchronization-techniques\" title=\"Anchor for Section 2. Thread Synchronization Techniques\">#<\/a><\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn various techniques to synchronize between threads using a lock, event, thread-safe queue, and semaphore.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-threading-lock\/\">Lock<\/a> &#8211; show you how to use a lock object to control access to a shared variable safely from multiple threads.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-threading-event\/\">Event<\/a> &#8211; learn how to use the threading Event to communicate between threads.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-stop-thread\/\">How to stop a thread<\/a> &#8211; learn how to stop a child thread from the main thread.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-semaphore\/\">Semaphore<\/a> &#8211; explain the semaphore concept and how to use the Semaphore object to limit the number of threads that can access a shared resource simultaneously.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='sharing-data-between-threads'>Section 3. Sharing Data Between Threads <a href=\"#sharing-data-between-threads\" class=\"anchor\" id=\"sharing-data-between-threads\" title=\"Anchor for Section 3. Sharing Data Between Threads\">#<\/a><\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn various techniques to share data between threads.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-thread-queue\/\">Thread-safe Queue<\/a> &#8211; show you how to use a thread-safe queue to exchange data safely between multiple threads.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='multiprocessing'>Section 4. Multiprocessing <a href=\"#multiprocessing\" class=\"anchor\" id=\"multiprocessing\" title=\"Anchor for Section 4. Multiprocessing\">#<\/a><\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to utilize the multiprocessing package to develop programs that run tasks in parallel.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-multiprocessing\/\">Multiprocessing<\/a> &#8211; show you how to run code in parallel using the multiprocessing module.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-processpoolexecutor\/\">Process Pools<\/a> &#8211; learn how to manage processes more efficiently by using a process pool.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='async-io'>Section 5. Async I\/O <a href=\"#async-io\" class=\"anchor\" id=\"async-io\" title=\"Anchor for Section 5. Async I\/O\">#<\/a><\/h2>\n\n\n\n<p>In this section, you&#8217;ll how to utilize concurrency provided by the <code>asyncio<\/code> package to improve program performance, throughput, and responsiveness.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-event-loop\/\">Understanding Event Loop<\/a> &#8211; explain how the event loop works and how <code>asyncio<\/code> package uses the event loop to achieve a single-threaded concurrency model.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-async-await\/\">async\/await<\/a> &#8211; introduce to you coroutines and how to use the async and await keywords to define and pause coroutines.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-create_task\/\">Creating tasks<\/a> &#8211; learn how to create tasks and schedule them for running on the event loop.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-cancel-tasks\/\">Canceling tasks<\/a> &#8211; show you how to cancel a task using the <code>cancel()<\/code> method of the <code>Task<\/code> object.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-wait_for\/\">Canceling a task with a timeout<\/a> &#8211; show you how to use the <code>asyncio.wait_for()<\/code> function to cancel a task with a timeout.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-wait\/\">asyncio.wait()<\/a> &#8211; learn about the <code>asyncio.wait()<\/code> function to run an iterable of awaitable objects concurrently.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-future\/\">Future<\/a> &#8211; explain to you the Future object and awaitables.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-asyncio-gather\/\">Running multiple tasks concurrently with gather()<\/a> &#8211; run a list of tasks concurrently with the <code>asyncio.gather()<\/code> function.<\/li>\n<\/ul>\n<\/div><\/div>\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=\"4104\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/\"\n\t\t\t\tdata-post-title=\"Python Concurrency\"\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=\"4104\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/\"\n\t\t\t\tdata-post-title=\"Python Concurrency\"\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 section, you&#8217;ll learn how to implement Python cocurrency using multithreading, multiprocessing, and asyncio package.<\/p>\n","protected":false},"author":1,"featured_media":4218,"parent":0,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4104","page","type-page","status-publish","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4104","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=4104"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4104\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media\/4218"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}