{"id":59498,"date":"2024-02-06T19:12:45","date_gmt":"2024-02-06T13:42:45","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=59498"},"modified":"2024-02-12T19:23:16","modified_gmt":"2024-02-12T13:53:16","slug":"speed-boost-how-multi-threading-makes-your-programs-faster","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/speed-boost-how-multi-threading-makes-your-programs-faster\/","title":{"rendered":"Speed Boost: How Multi-Threading Makes Your Programs Faster"},"content":{"rendered":"<p id=\"24e8\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\" data-selectable-paragraph=\"\">Imagine you\u2019re at a grocery store, and you need to buy a bunch of items. Now, you have two options:<\/p>\n<p id=\"fbda\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\" data-selectable-paragraph=\"\"><strong class=\"wo fu\">Option 1:<\/strong>\u00a0You\u2019re the only person shopping, and you have to go through each aisle one by one, picking up items and waiting in line at the checkout each time. It\u2019s like a slow-motion shopping marathon, and you end up spending a lot of time.<\/p>\n<p id=\"f1b2\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\" data-selectable-paragraph=\"\"><strong class=\"wo fu\">Option 2:<\/strong>\u00a0You bring a friend (or a few friends) with you. Each person takes a separate aisle, grabs items simultaneously, and you all meet up at the checkout. It\u2019s like a synchronized shopping dance, and you finish much faster!<\/p>\n<p id=\"9b90\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\" data-selectable-paragraph=\"\">Now, replace \u201cyou\u201d with a computer program, \u201caisles\u201d with tasks, and \u201cfriends\u201d with threads. That\u2019s essentially multi-threading! In the computer world, programs have different tasks to perform, and multi-threading allows them to tackle those tasks simultaneously, making the whole process faster and more efficient.<\/p>\n<h2 id=\"969a\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\"><strong class=\"wo fu\">In super easy language, what exactly is a thread?<\/strong><\/h2>\n<p id=\"b397\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\" data-selectable-paragraph=\"\">Think of a thread as a helper inside your computer that can do one thing at a time. If your computer is like a kitchen, a thread is like a little chef handling one task, and when you have many chefs (threads), the kitchen gets things done quicker!<\/p>\n<h2 id=\"be66\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\"><strong class=\"wo fu\">The thread has to go through various stages of its life<\/strong><\/h2>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:4800\/format:webp\/1*F4zhyqwTTiTPqFfYZi-rjg.jpeg\" \/><\/p>\n<div class=\"ft rz sa sb sc\">\n<div class=\"ab cc\">\n<div class=\"dj bg rl rm rn ro\">\n<p><strong class=\"wo fu\">1. Getting Ready (New): <\/strong>The thread is created but hasn\u2019t started yet.<\/p>\n<p><strong class=\"wo fu\">2. Time to Work (Runnable\/Ready): <\/strong>Now the Task is assigned, the thread is all set to start the job.<\/p>\n<p><strong class=\"wo fu\">3. Working Hard (Running\/Active): <\/strong>The thread is actively doing its task, just like a worker doing their job.<\/p>\n<p><strong class=\"wo fu\">4. Taking a Break (Waiting\/Blocked): <\/strong>Sometimes, the thread needs to wait a bit, like taking a short break until things are ready.<\/p>\n<p><strong class=\"wo fu\">5. Job Done (Dead\/Terminiated): <\/strong>Finally, the thread finishes its job and is done for the day, just like a worker going home after work.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div role=\"separator\">\n<h2 id=\"90b5\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\"><strong class=\"wo fu\">How things can be implemented using Python<\/strong><\/h2>\n<p id=\"d0a2\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\" data-selectable-paragraph=\"\">Python makes things easier with a built-in module <code class=\"db yf yg yh yi b\">threading<\/code>\u00a0which takes care of everything.<\/p>\n<p class=\"xk xl xm xn xo yj yi yk bo yl ba bj\"><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/1.png\" \/><\/p>\n<p id=\"8232\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\" data-selectable-paragraph=\"\">The thread class is all you need for creating and using multiple threads in your Python program. There are various ways by which we can create threads<\/p>\n<h2 id=\"5e96\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\"><strong class=\"wo fu\">Firstly, let&#8217;s create a thread by <\/strong><code class=\"db yf yg yh yi b\"><strong class=\"wo fu\">Thread<\/strong><\/code><strong class=\"wo fu\">\u00a0class object<\/strong><\/h2>\n<\/div>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/2.png\" \/><\/p>\n<p id=\"2503\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\" data-selectable-paragraph=\"\">Here is another way of creating the thread is using Inheritance; let&#8217;s see how:<\/p>\n<p class=\"xk xl xm xn xo yj yi yk bo yl ba bj\"><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/3.png\" \/><\/p>\n<h2 class=\"xk xl xm xn xo yj yi yk bo yl ba bj\"><strong class=\"wo fu\">Summary<\/strong><\/h2>\n<p id=\"b777\" class=\"pw-post-body-paragraph wm wn se wo b wp wq wr ws wt wu wv ww ov wx wy wz oy xa xb xc pb xd xe xf xg ft bj\" data-selectable-paragraph=\"\">To sum it up, in Python, threading is like having multiple workers (threads) doing different jobs at the same time. The program creates a custom thread to perform a specific task concurrently with the main thread. Each thread works independently, allowing the main thread to continue its own tasks. Threading is a handy tool to make programs faster by multitasking effectively.<\/p>\n<p><span data-preserver-spaces=\"true\"><strong>Related Video<\/strong>\u00a0<\/span><a class=\"editor-rtfLink\" href=\"https:\/\/www.youtube.com\/watch?v=GCsCnBnXly8\" target=\"_blank\" rel=\"noopener\"><span data-preserver-spaces=\"true\">https:\/\/www.youtube.com\/watch?v=GCsCnBnXly8<\/span><\/a><\/p>\n<p><strong><span data-preserver-spaces=\"true\">Official Website<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0<\/span><a class=\"editor-rtfLink\" href=\"https:\/\/pythonbricks.pythonanywhere.com\/\" target=\"_blank\" rel=\"noopener\"><span data-preserver-spaces=\"true\">https:\/\/pythonbricks.pythonanywhere.com\/<\/span><\/a><\/p>\n<p><strong><span data-preserver-spaces=\"true\">Youtube Channel<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0<\/span><a class=\"editor-rtfLink\" href=\"https:\/\/www.youtube.com\/@PythonBricks\" target=\"_blank\" rel=\"noopener\"><span data-preserver-spaces=\"true\">https:\/\/www.youtube.com\/@PythonBricks<\/span><\/a><\/p>\n<p><strong><span data-preserver-spaces=\"true\">Telegram<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0<\/span><a class=\"editor-rtfLink\" href=\"https:\/\/t.me\/pythonbricks\" target=\"_blank\" rel=\"noopener\"><span data-preserver-spaces=\"true\">https:\/\/t.me\/pythonbricks<\/span><\/a><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Imagine you\u2019re at a grocery store, and you need to buy a bunch of items. Now, you have two options: Option 1:\u00a0You\u2019re the only person shopping, and you have to go through each aisle one by one, picking up items and waiting in line at the checkout each time. It\u2019s like a slow-motion shopping marathon, [&hellip;]<\/p>\n","protected":false},"author":1695,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1994],"tags":[5644,5645,3747,1358,5647,5646],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59498"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1695"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=59498"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59498\/revisions"}],"predecessor-version":[{"id":60305,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59498\/revisions\/60305"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=59498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=59498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=59498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}