{"id":4085,"date":"2022-07-14T09:52:13","date_gmt":"2022-07-14T09:52:13","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4085"},"modified":"2023-06-04T02:29:27","modified_gmt":"2023-06-04T02:29:27","slug":"python-thread-queue","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-concurrency\/python-thread-queue\/","title":{"rendered":"Python Thread-safe Queue"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use a Python thread-safe queue to exchange data safely between multiple threads.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-thread-safe-queue'>Introduction to the Python thread-safe queue <a href=\"#introduction-to-the-python-thread-safe-queue\" class=\"anchor\" id=\"introduction-to-the-python-thread-safe-queue\" title=\"Anchor for Introduction to the Python thread-safe queue\">#<\/a><\/h2>\n\n\n\n<p>The built-in <code>queue<\/code> module allows you to exchange data safely between <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-thread\/\">multiple threads<\/a>. The <code>Queue<\/code> class in the <code>queue<\/code> module implements all required locking semantics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='creating-a-new-queue'>Creating a new queue <a href=\"#creating-a-new-queue\" class=\"anchor\" id=\"creating-a-new-queue\" title=\"Anchor for Creating a new queue\">#<\/a><\/h3>\n\n\n\n<p>To create a new queue, you import the Queue class from the queue module: <\/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\">from<\/span> queue <span class=\"hljs-keyword\">import<\/span> Queue<\/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<p>and use the <code>Queue<\/code> constructor as follows:<\/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\">queue = Queue()<\/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<p>To create a queue with a size limit, you can use the <code>maxsize<\/code> parameter. For example, the following creates a queue that can store up to 10 items:<\/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\">queue = Queue(maxsize=<span class=\"hljs-number\">10<\/span>)<\/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\" id='adding-an-item-to-the-queue'>Adding an item to the queue <a href=\"#adding-an-item-to-the-queue\" class=\"anchor\" id=\"adding-an-item-to-the-queue\" title=\"Anchor for Adding an item to the queue\">#<\/a><\/h3>\n\n\n\n<p>To add an item to the queue, you use the <code>put()<\/code> method like this:<\/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\">queue.add(item)<\/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<p>Once the queue is full, you won&#8217;t be able to add an item to it. Also, the call to the <code>put()<\/code> method will block until the queue has space available.<\/p>\n\n\n\n<p>If you don&#8217;t want the <code>put()<\/code> method to block if the queue is full, you can set the <code>block<\/code> argument to <code>False<\/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\">queue.put(item, block=<span class=\"hljs-literal\">False<\/span>)<\/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<p>In this case, the <code>put()<\/code> method will raise the <code>queue.Full<\/code> exception if the queue is full:<\/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\">try<\/span>:\n   queue.put(item, block=<span class=\"hljs-literal\">False<\/span>)\n<span class=\"hljs-keyword\">except<\/span> queue.Full <span class=\"hljs-keyword\">as<\/span> e:\n   <span class=\"hljs-comment\"># handle exceptoin<\/span><\/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<p>To add an item to a sized limited queue and block with a timeout, you can use the <code>timeout<\/code> parameter like this:<\/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\">try<\/span>:\n   queue.put(item, timeout=<span class=\"hljs-number\">3<\/span>)\n<span class=\"hljs-keyword\">except<\/span> queue.Full <span class=\"hljs-keyword\">as<\/span> e:\n   <span class=\"hljs-comment\"># handle exceptoin<\/span><\/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<h3 class=\"wp-block-heading\" id='getting-an-item-from-the-queue'>Getting an item from the queue <a href=\"#getting-an-item-from-the-queue\" class=\"anchor\" id=\"getting-an-item-from-the-queue\" title=\"Anchor for Getting an item from the queue\">#<\/a><\/h3>\n\n\n\n<p>To get an item from the queue, you can use the <code>get()<\/code> method:<\/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\">item = queue.get()<\/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<p>The <code>get()<\/code> method will block until an item is available for retrieval from the queue.<\/p>\n\n\n\n<p>To get an item from the queue without blocking, you can set the block parameter to <code>False<\/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\">try<\/span>:\n   queue.get(block=<span class=\"hljs-literal\">False<\/span>)\n<span class=\"hljs-keyword\">except<\/span> queue.Empty:\n   <span class=\"hljs-comment\"># handle exception<\/span><\/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<p>To get an item from the queue and block it with a time limit, you can use the <code>get()<\/code> method with a timeout:<\/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\"><span class=\"hljs-keyword\">try<\/span>:\n   item = queue.get(timeout=<span class=\"hljs-number\">10<\/span>)\n<span class=\"hljs-keyword\">except<\/span> queue.Empty:\n   <span class=\"hljs-comment\"># ...<\/span><\/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<h3 class=\"wp-block-heading\" id='getting-the-size-of-the-queue'>Getting the size of the queue <a href=\"#getting-the-size-of-the-queue\" class=\"anchor\" id=\"getting-the-size-of-the-queue\" title=\"Anchor for Getting the size of the queue\">#<\/a><\/h3>\n\n\n\n<p>The <code>qsize()<\/code> method returns the number of items in the queue:<\/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\">size = queue.size()<\/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<p>Also, the <code>empty()<\/code> method returns <code>True<\/code> if the queue is empty or False otherwise. On the other hand, the <code>full()<\/code> method returns True if the queue is full or <code>False<\/code> otherwise.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='marking-a-task-as-completed'>Marking a task as completed <a href=\"#marking-a-task-as-completed\" class=\"anchor\" id=\"marking-a-task-as-completed\" title=\"Anchor for Marking a task as completed\">#<\/a><\/h3>\n\n\n\n<p>An item that you add to the queue represents a unit of work or a task. <\/p>\n\n\n\n<p>When a thread calls the <code>get()<\/code> method to get the item from the queue, it may need to process it before the task is considered completed.<\/p>\n\n\n\n<p>Once completed, the thread may call the <code>task_done()<\/code> method of the queue to indicate that it has processed the task completely:<\/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\">item = queue.get()\n\n<span class=\"hljs-comment\"># process the item<\/span>\n<span class=\"hljs-comment\"># ...<\/span>\n\n<span class=\"hljs-comment\"># mark the item as completed<\/span>\nqueue.task_done()<\/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<h3 class=\"wp-block-heading\" id='waiting-for-all-tasks-on-the-queue-to-be-completed'>Waiting for all tasks on the queue to be completed <a href=\"#waiting-for-all-tasks-on-the-queue-to-be-completed\" class=\"anchor\" id=\"waiting-for-all-tasks-on-the-queue-to-be-completed\" title=\"Anchor for Waiting for all tasks on the queue to be completed\">#<\/a><\/h3>\n\n\n\n<p>To wait for all tasks on the queue to be completed, you can call the <code>join()<\/code> method on the queue object:<\/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\">queue.join()<\/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\" id='python-thread-safe-queue-example'>Python thread-safe queue example <a href=\"#python-thread-safe-queue-example\" class=\"anchor\" id=\"python-thread-safe-queue-example\" title=\"Anchor for Python thread-safe queue example\">#<\/a><\/h2>\n\n\n\n<p>The following example illustrates how to use the thread-safe queue to exchange data between two threads:<\/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> time\n<span class=\"hljs-keyword\">from<\/span> queue <span class=\"hljs-keyword\">import<\/span> Empty, Queue\n<span class=\"hljs-keyword\">from<\/span> threading <span class=\"hljs-keyword\">import<\/span> Thread\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">producer<\/span><span class=\"hljs-params\">(queue)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">6<\/span>):\n        print(<span class=\"hljs-string\">f'Inserting item <span class=\"hljs-subst\">{i}<\/span> into the queue'<\/span>)\n        time.sleep(<span class=\"hljs-number\">1<\/span>)\n        queue.put(i)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">consumer<\/span><span class=\"hljs-params\">(queue)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-literal\">True<\/span>:\n        <span class=\"hljs-keyword\">try<\/span>:\n            item = queue.get()\n        <span class=\"hljs-keyword\">except<\/span> Empty:\n            <span class=\"hljs-keyword\">continue<\/span>\n        <span class=\"hljs-keyword\">else<\/span>:\n            print(<span class=\"hljs-string\">f'Processing item <span class=\"hljs-subst\">{item}<\/span>'<\/span>)\n            time.sleep(<span class=\"hljs-number\">2<\/span>)\n            queue.task_done()\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    queue = Queue()\n\n    <span class=\"hljs-comment\"># create a producer thread and start it<\/span>\n    producer_thread = Thread(\n        target=producer,\n        args=(queue,)\n    )\n    producer_thread.start()\n\n    <span class=\"hljs-comment\"># create a consumer thread and start it<\/span>\n    consumer_thread = Thread(\n        target=consumer,\n        args=(queue,),\n        daemon=<span class=\"hljs-literal\">True<\/span>\n    )\n    consumer_thread.start()\n\n    <span class=\"hljs-comment\"># wait for all tasks to be added to the queue<\/span>\n    producer_thread.join()\n\n    <span class=\"hljs-comment\"># wait for all tasks on the queue to be completed<\/span>\n    queue.join()\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    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<p>How it works.<\/p>\n\n\n\n<p>First, define the <code>producer()<\/code> function that adds numbers from 1 to 11 to the queue. It delays one second in each iteration:<\/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-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">producer<\/span><span class=\"hljs-params\">(queue)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">6<\/span>):\n        print(<span class=\"hljs-string\">f'Inserting item <span class=\"hljs-subst\">{i}<\/span> into the queue'<\/span>)\n        time.sleep(<span class=\"hljs-number\">1<\/span>)\n        queue.put(i)<\/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<p>Second, define the <code>consumer()<\/code> function that gets an item from the queue and processes it. It delays two seconds after processing each item on the queue:<\/p>\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-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">consumer<\/span><span class=\"hljs-params\">(queue)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-literal\">True<\/span>:\n        <span class=\"hljs-keyword\">try<\/span>:\n            item = queue.get()\n        <span class=\"hljs-keyword\">except<\/span> Empty:\n            <span class=\"hljs-keyword\">continue<\/span>\n        <span class=\"hljs-keyword\">else<\/span>:\n            print(<span class=\"hljs-string\">f'Processing item <span class=\"hljs-subst\">{item}<\/span>'<\/span>)\n            time.sleep(<span class=\"hljs-number\">2<\/span>)\n            queue.task_done()<\/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<p>The queue.<code>task_done()<\/code> indicates that the function has processed the item on the queue.<\/p>\n\n\n\n<p>Third, define the <code>main()<\/code> function that creates two threads, one thread adds a number to the queue every second while another thread processes an item on the queue every two seconds:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    queue = Queue()\n\n    <span class=\"hljs-comment\"># create a producer thread and start it<\/span>\n    producer_thread = Thread(\n        target=producer,\n        args=(queue,)\n    )\n    producer_thread.start()\n\n    <span class=\"hljs-comment\"># create a consumer thread and start it<\/span>\n    consumer_thread = Thread(\n        target=consumer,\n        args=(queue,),\n        daemon=<span class=\"hljs-literal\">True<\/span>\n    )\n    consumer_thread.start()\n\n    <span class=\"hljs-comment\"># wait for all tasks to be added to the queue<\/span>\n    producer_thread.join()\n\n    <span class=\"hljs-comment\"># wait for all tasks on the queue to be completed<\/span>\n    queue.join()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><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<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Inserting item <span class=\"hljs-number\">1<\/span> into the queue\nInserting item <span class=\"hljs-number\">2<\/span> into the queue\nProcessing item <span class=\"hljs-number\">1<\/span>\nInserting item <span class=\"hljs-number\">3<\/span> into the queue\nProcessing item <span class=\"hljs-number\">2<\/span>\nInserting item <span class=\"hljs-number\">4<\/span> into the queue\nInserting item <span class=\"hljs-number\">5<\/span> into the queue\nProcessing item <span class=\"hljs-number\">3<\/span>\nProcessing item <span class=\"hljs-number\">4<\/span>\nProcessing item <span class=\"hljs-number\">5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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<p>The following are steps in the <code>main()<\/code> function:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a new queue by calling the <code>Queue()<\/code> constructor<\/li>\n\n\n\n<li>Create a new thread called <code>producer_thread<\/code> and start it immediately<\/li>\n\n\n\n<li>Create a <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-daemon-threads\/\">daemon thread<\/a> called <code>consumer_thread<\/code> and start it immediately.<\/li>\n\n\n\n<li>Wait for all the numbers to be added to the queue using the <code>join()<\/code> method of the thread.<\/li>\n\n\n\n<li>Wait for all the tasks on the queue to be completed by calling the <code>join()<\/code> method of the queue.<\/li>\n<\/ol>\n\n\n\n<p>The producer adds a number to the queue every second, and the consumer process a number from the queue every two seconds. It also displays the numbers on the queue every second.<\/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>Use the <code>Queue<\/code> class of the <code>queue<\/code> module to safely exchange data between multiple threads.<\/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=\"4085\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-thread-queue\/\"\n\t\t\t\tdata-post-title=\"Python Thread-safe Queue\"\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=\"4085\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-thread-queue\/\"\n\t\t\t\tdata-post-title=\"Python Thread-safe Queue\"\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 how to use a Python thread-safe queue to exchange data safely between multiple threads.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4104,"menu_order":11,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4085","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4085","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=4085"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4085\/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=4085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}