<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Madhusudan Miniyar on Medium]]></title>
        <description><![CDATA[Stories by Madhusudan Miniyar on Medium]]></description>
        <link>https://medium.com/@madhuminiyar?source=rss-34737b2555a5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*CK0qqp9icH3f28Op8jHbzw.jpeg</url>
            <title>Stories by Madhusudan Miniyar on Medium</title>
            <link>https://medium.com/@madhuminiyar?source=rss-34737b2555a5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 14:35:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@madhuminiyar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to Multithreading and Multiprocessing in Python]]></title>
            <link>https://medium.com/@madhuminiyar/introduction-to-multithreading-and-multiprocessing-in-python-91a725afb216?source=rss-34737b2555a5------2</link>
            <guid isPermaLink="false">https://medium.com/p/91a725afb216</guid>
            <dc:creator><![CDATA[Madhusudan Miniyar]]></dc:creator>
            <pubDate>Mon, 27 Apr 2020 16:56:44 GMT</pubDate>
            <atom:updated>2020-04-27T18:14:29.474Z</atom:updated>
            <content:encoded><![CDATA[<p>Before going to these concepts let’s get to know about the some terms</p><ol><li><strong>Parallelism</strong> is the idea in which a single task is divided into two or more sub-task to process simultaneously to expedite overall execution.</li><li><strong>Concurrency</strong> is the computation of processes within a time frame to give an impression of simultaneous execution</li></ol><p>Perhaps, people opt for the multithreading and multiprocessing to make execution fast.</p><p>Multiprocessing refers to the ability of a system to support more than one processor at the same time. While Multithreading is the ability of a program or an operating system process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having to have multiple copies of the programming running in the computer.</p><ol><li><strong>Multithreading: Thread based parallelism</strong></li></ol><ul><li>In the case of multithreading<strong>,</strong> we create the thread from the existing process which will lead to sharing the memory between them.</li><li>All created threads share the single GIL [ Global Interpreter Lock] for all the threads.</li><li>So, we use <strong>ThreadpoolExecutor</strong> from <strong>concurrent.futures </strong>module to create the multiple threads to execute the block of code and achieve parallelism.</li><li>Here is an example of multithreading:</li></ul><pre>from concurrent.futures import ThreadPoolExecutor</pre><pre>def task(i):<br>    print(i ** 2)<br>    print(i * i * i)<br>    while i &lt; 10000000:<br>        i = i + 1<br>    return</pre><pre>if __name__ == &quot;__main__&quot;:<br>    executor = ThreadPoolExecutor(4)<br>    for i in range(20):<br>        executor.submit(task, (i))</pre><p>2. <strong>Multiprocessing: Process-based parallelism</strong></p><ul><li>In the case of multiprocessing<strong>,</strong> we create a new process independently. As the process is created independently it won’t share a memory with any other process.</li><li>All processes have different GIL [ Global Interpreter Lock].</li><li>So, we use <strong>ProcesspoolExecutor</strong> from <strong>concurrent.futures</strong> module to create multiple processes to execute the block of code and achieve parallelism.</li><li>Here is an example of multiprocessing:</li></ul><pre>from concurrent.futures import ProcessPoolExecutor</pre><pre>def task(i):<br>    print(i ** 2)<br>    print(i * i * i)<br>    while i &lt; 10000000:<br>        i = i + 1<br>    return</pre><pre>if __name__ == &quot;__main__&quot;:<br>    executor = ProcessPoolExecutor(4)<br>    for i in range(20):<br>        executor.submit(task, (i))</pre><p>But here are some interesting facts and conclusions about the parallelism:</p><ol><li>Even we use the multiple threads to achieve parallelism the program will execute serial manner this is because of GIL. <strong>GIL is the mechanism used by an interpreter to assure that only one thread executes the python bytecode at a time. </strong>As thread shares the single GIL only one thread will execute at one time till the time other threads will wait to complete the execution and get the lock over GIL.</li><li>As GIL is always released while doing the I/O we can use multithreading in IO to achieve parallelism.</li><li>So in the case of<strong> IO</strong> to achieve parallelism we must use multithreading as it is faster than multiprocessing because multiprocessing involves the overhead of creating the processes. While in other cases we should prefer multiprocessing.</li><li>The number of processes and threads should be decided on the number of cores to overhead.In general <strong>no_of_process = no_of_cores</strong> and <strong>no_of_threads = a * no_of_cores</strong> where a = any random value choose on trial and error basis</li></ol><p>Hence we should prefer Multithreading in case of IO otherwise we should use multiprocessing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91a725afb216" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>