<?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 Ankur Jain on Medium]]></title>
        <description><![CDATA[Stories by Ankur Jain on Medium]]></description>
        <link>https://medium.com/@ankurj630?source=rss-2aaa352e7815------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*A4-8qQehIujI4mx0rJZhBw.jpeg</url>
            <title>Stories by Ankur Jain on Medium</title>
            <link>https://medium.com/@ankurj630?source=rss-2aaa352e7815------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:15:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ankurj630/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[Multi threading & processing de-mystified in python]]></title>
            <link>https://medium.com/@ankurj630/multi-threading-processing-de-mystified-in-python-1e53022dda7d?source=rss-2aaa352e7815------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e53022dda7d</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[threading]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ankur Jain]]></dc:creator>
            <pubDate>Sat, 04 Aug 2018 06:21:53 GMT</pubDate>
            <atom:updated>2018-08-04T06:21:53.201Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*jtwRpitFzZUt26lJYoyU4A.png" /><figcaption>source dribble</figcaption></figure><p>Before we being with threading and processing, I want to make talk about two important terms</p><ol><li>Parallelism</li><li>Concurrency</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*aXS8l2T0XPGVlQdcclNjcA.jpeg" /></figure><p><strong><em>Parallelism</em> </strong>is basically similar stuff that’s happening at the same time.</p><p>Let’s understand this with the help of an example. Assume that I need to complete my science and mathematics homework. The serial way of doing this is to first complete the science homework and then the mathematics homework.</p><p>But in parallelism, I would take help of my friend. I will complete the science homework and he will complete the mathematics. This implies that both of the homework are happening simultaneously and hence parallel</p><p>But <strong><em>Concurrency</em> </strong>is a false feeling of parallelism. By false I means that it shows that both the process are happening simultaneously but in fact they are happening in serial manner. This is achieved by intelligent and fast switching between the two processes.</p><p>In above example, if I keep on switching between my science and mathematics homework, then it will give a false feeling that both the homework are happening simultaneously but in reality they are not.</p><p>Now since we know what’s the difference between parallelism and concurrency, we can talk about <strong>threading</strong> and <strong>processing</strong>. In python multi-threading works on concurrency and multi-processing works on parallelism. Each of them have their pros and cons which we are gonna discuss in details.</p><h4># Multi Processing (CPU bound task)</h4><p>Multi processing is achieved in python by starting multiple instances of python interpreter to do the desired tasks. These python interpreter are allocated their own individual resources which include memory, cpu and others if needed.</p><p>In python multi processing can be easily implemented with the help of <strong>multiprocessing</strong> module.</p><p>Let’s take an example to add first 10 numbers by breaking it down into two processes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ea1018d818c783523cb77448d398054/href">https://medium.com/media/0ea1018d818c783523cb77448d398054/href</a></iframe><pre>$ python3 multi_processing.py<br>The final sum is 55</pre><p><strong>What’s Happening!</strong></p><ol><li>We are creating a pool of two processes using <strong>multiprocessing.Pool()</strong>. Which basically means that there are going to be two process running.</li><li>Now we are assigning each process a handler method( the function to be run) and additional arguments for that function. This is done using <strong>pool.map()</strong>. This also combines the result from all the processes and return a list.</li><li>Now we close the pool using <strong>pool.close()</strong> and asks the main thread to wait till all the process in the pool are completed to get our result back by using <strong>pool.join()</strong></li><li>We are printing the result.</li></ol><p><strong>Pros</strong></p><ol><li>Takes advantage of multiple CPUs &amp; cores.</li><li>Separate memory space</li><li>Multi processing is fast because each process is running independently.</li></ol><p><strong>Cons</strong></p><ol><li>New process are heavy and take time to spawn up.</li><li>Have a larger memory footprint.</li></ol><p><strong>Take Away</strong></p><p>Use multi processing when you have enough resources to allocate and your work highly depends upon cpu utilization rather than i/o.</p><h4># Multi threading (I/O bound task)</h4><p>The threads are lightweight processes. The idea behind threads is to achieve parallelism by dividing a task into multiple threads.</p><p>The basic difference between threads and process is that, the threads are a part of a single process and hence run in a shared memory space while the process run in separate memory space.</p><p>But you will heard that the threads in python and not <strong>TRUE </strong>threads. By TRUE they meant that unlike Java not more than one thread can be running at a single time. The reason for such behavior is GIL ( Global Interpreter Lock ).</p><p>GIL allows only one thread to acquire the interpretor and execute its code. When the running thread releases the lock, only then another thread acquires it and run. Sounds like <strong>concurrency </strong>right!</p><p>Let’s look an example of multi-threading. We are using the same example as above</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20bc9271656ec9785d0ca45f08b76191/href">https://medium.com/media/20bc9271656ec9785d0ca45f08b76191/href</a></iframe><pre>$ python3 multi_threading.py<br>The final sum is 55</pre><p><strong>What’s Happening!</strong></p><ol><li>We are creating a pool of two threads using <strong>ThreadPool()</strong>.</li><li>Now we are assigning each thread a handler method( the function to be run) and additional arguments for that function. This is done using <strong>pool.map()</strong>. This also combines the result from all the processes and return a list.</li><li>Now we close the pool using <strong>pool.close().</strong></li><li>We are printing the result.</li></ol><p>But now the question arises <strong>why and when we should use thread?</strong> The threads in python are beneficial when you have an i/o bound task. Because when your thread is making an i/o request for eg (database query, http call ) then it releases the lock and make such requests in the background which increases the overall execution speed significantly.</p><p><strong>Pros</strong></p><ol><li>Lightweight — low memory footprint</li><li>Shared memory — makes access to state from another context easier</li><li>Great option for I/O-bound applications</li></ol><p><strong>Cons</strong></p><ol><li>Code is usually harder to understand and to get right — the potential for race conditions increases dramatically</li><li>Subject to the GIL</li></ol><p><strong>Take Away</strong></p><p>Use multi threading when your work highly depends upon i/o rather than cpu utilization.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e53022dda7d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Django-Rok : Public URL for your local web server]]></title>
            <link>https://medium.com/@ankurj630/django-rok-public-url-for-your-local-web-server-fec89e635282?source=rss-2aaa352e7815------2</link>
            <guid isPermaLink="false">https://medium.com/p/fec89e635282</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[ngrok]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[webhooks]]></category>
            <category><![CDATA[ssh]]></category>
            <dc:creator><![CDATA[Ankur Jain]]></dc:creator>
            <pubDate>Sat, 28 Jul 2018 18:51:10 GMT</pubDate>
            <atom:updated>2018-07-28T18:51:10.168Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*QoQPjR1HmLyUMgAgdx2r9Q.jpeg" /></figure><blockquote>The <a href="https://github.com/droidlife/django-rok">django-rok</a> is an <strong>reverse-ssh tunneling </strong>tool provides a public URL for your local web server. This is helpful in web-hook testing, quick UAT and many more.</blockquote><p>To set up a SSH tunnel a given port of one machine needs to be forwarded to a port in the other machine which will be the other end of the tunnel. Once the SSH tunnel has been established, the user can connect to earlier specified port at first machine to access the network service.</p><h4># Setting Up</h4><p>Let’s begin by setting up django_rok</p><p>1. Install <strong>djanog_rok </strong>using pip.</p><pre>$ pip install django_rok<br>or<br>pip3 install django_rok</pre><p>2. Add django_rok to installed app</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*QhLxZyeOtG1_6rVhrJ2iiw.png" /></figure><p>3. Add following parameters to the settings.py file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*1xe0cG4S8D97LvmO3CdwQA.png" /></figure><p>4. Enable Remote Port forwarding on the <strong>server</strong>. This can be achieved by editing<strong> /etc/ssh/sshd_config .</strong></p><pre>$ sudo vim /etc/ssh/sshd_config</pre><p>Add the following line somewhere in that config file. Make sure you add it only once!</p><pre>GatewayPorts yes</pre><p>Now restart the service</p><pre>$ sudo service ssh restart</pre><h4># Running</h4><p>The django_rok can be run by the following command</p><pre>$ python manage.py runrok</pre><p>This starts the django server as well as the reverse ssh tunnel</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fUnNbze2GA0wOclt2kMPZw.png" /></figure><p>Now go to <a href="http://192.168.1.1:9000/"><strong>http://192.168.1.1:9000</strong></a><strong> </strong>to access your local code on a public URL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UXMCnRCBUDhPu9G7wZdtqw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fec89e635282" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>