<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nayan Das</title>
    <description>The latest articles on DEV Community by Nayan Das (@mrpalindrome).</description>
    <link>https://dev.to/mrpalindrome</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F725948%2Fa0698c1f-6024-44cb-b9a5-b26b628cb983.png</url>
      <title>DEV Community: Nayan Das</title>
      <link>https://dev.to/mrpalindrome</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrpalindrome"/>
    <language>en</language>
    <item>
      <title>Multithreading to Memory leak! Why?</title>
      <dc:creator>Nayan Das</dc:creator>
      <pubDate>Thu, 23 Mar 2023 19:10:31 +0000</pubDate>
      <link>https://dev.to/mrpalindrome/multithreading-to-memory-leak-why-3m1</link>
      <guid>https://dev.to/mrpalindrome/multithreading-to-memory-leak-why-3m1</guid>
      <description>&lt;p&gt;Everything has it's own pros and cons! Even your most favourite language like Python. One of the most known feature of Python is &lt;a href="https://wiki.python.org/moin/GlobalInterpreterLock" rel="noopener noreferrer"&gt;GIL&lt;/a&gt; which stands for "Global Interpreter Lock". It is a mechanism that is used in the CPython interpreter to synchronize the execution of threads so that only one thread can execute Python bytecode at a time. This means that even on a multi-core processor, only one thread can execute Python code at a time.&lt;/p&gt;

&lt;p&gt;Back then &lt;a href="https://wiki.python.org/moin/GlobalInterpreterLock" rel="noopener noreferrer"&gt;GIL&lt;/a&gt; was made in order to simplify the implementation of the CPython interpretor. The GIL make it easier to write thread-safe CPython extensions, but it also limits the parallelism of Python programs and became performance bottleneck for CPU-bound programs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsvz3tcak1l1c07fgjd0f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsvz3tcak1l1c07fgjd0f.png" alt="Demonstration of GIL"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then how is this related to memory leak while multithreading? Why am I talking about &lt;a href="https://wiki.python.org/moin/GlobalInterpreterLock" rel="noopener noreferrer"&gt;GIL&lt;/a&gt;? Nice question!&lt;/p&gt;

&lt;p&gt;Before getting into it we need to understand when we should use multithreading or multiprocessing or asyncio in Python. When a task is CPU-bound, multiprocessing should be used, when it is fast and I/O-bound with a small number of connections, multithreading should be used, and when it is slow and I/O-bound with a large number of connections, asyncio should be used.&lt;/p&gt;

&lt;p&gt;Here is a program to demonstrate multithreading in python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Multithreading.py
from threading import Thread
import time

def func(item):
    time.sleep(item)

start = time.perf_counter()
batch_processes = []

for i in list(range(1,13)):
    p = Thread(target=func, args=(i,))
    batch_processes.append(p)
    p.start()
for p in batch_processes:
    p.join()
finish = time.perf_counter()
print (f'Finished in {round(finish-start, 2)} second(s)')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# OUTPUT
Finished in 12.01 second(s)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, how can GIL affects the memory leak in Python. To be concise it is not properly memory leak. When running any I/O-bound Python programmes, such as any API calls, writing to and reading from disc creating connection to web servers or database clients, the GIL will always be released, allowing other threads to run Python code during this time. This is because these programmes spend the majority of their time waiting for I/O operations to complete.&lt;/p&gt;

&lt;p&gt;But suppose you are using multithreading and loading a tone of heavy libraries while creating multiple connections with different apis or databases as well as storing them can lead to memory leak because at that time there will be a situation when the thread will be sleeping for 1 or something seconds and the GIL will get released and it will start the next thread in the mean time which will repeat the whole process of loading heavy libraries and creating connections with APIs and databases and it will take up the memory ones again and the memory will not release until the process inside the thread is completed.&lt;/p&gt;

&lt;p&gt;Same if we run any highly cpu bounded task which also include some I/O bound task in multithreading it will also take up extra memory due to incomplete threads when GIL get released at any point of time which will led to high memory leak.&lt;/p&gt;

&lt;p&gt;This is what I faced and while researching more about this topic of GIL and multithreading I concluded the above information. Please be sure to share any difficulties you have encountered while working with multiple threads in Python.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>AtlasHackathon: Explore With Us (A Web App using Django, MongoDB Atlas)</title>
      <dc:creator>Nayan Das</dc:creator>
      <pubDate>Mon, 10 Jan 2022 19:01:10 +0000</pubDate>
      <link>https://dev.to/mrpalindrome/atlashackathon-explore-with-us-a-web-app-using-django-mongodb-atlas-5bj0</link>
      <guid>https://dev.to/mrpalindrome/atlashackathon-explore-with-us-a-web-app-using-django-mongodb-atlas-5bj0</guid>
      <description>&lt;h3&gt;
  
  
  Overview of My Submission
&lt;/h3&gt;

&lt;p&gt;Explore With Us is a vacation destination catalog website build with Django, Bootstrap and MongoDB Atlas.&lt;br&gt;
I created this webapp to take part in MongoDB Atlas Hackathon.&lt;/p&gt;
&lt;h4&gt;
  
  
  Technologies I used:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;HTML, CSS, JavaScript and Bootstrap 4.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Django&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;MongoDB Atlas&lt;/p&gt;
&lt;ul&gt;

&lt;li&gt;I used MongoDB Atlas to store the database by creating a cluster on  &lt;a href="https://www.mongodb.com/cloud/atlas/register"&gt;MongoDB&lt;/a&gt; &lt;/li&gt;

&lt;li&gt;I used the Atlas search feature to implement a fast, relevance-based full-text search for the destinations that I have in my database.&lt;/li&gt;

&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  How I used Atlas Search feature:
&lt;/h4&gt;

&lt;p&gt;I used MongoDB Atlas search to implement the autocomplete feature in my "Search Destination" Search Bar.&lt;br&gt;
First I made a search index for my collection "main_destination"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xTQdCFmL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ograz5f6ousy32oxa6m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xTQdCFmL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ograz5f6ousy32oxa6m5.png" alt="All collections in the database Project_data" width="880" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Search Index I used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": [
        {
          "foldDiacritics": false,
          "maxGrams": 7,
          "minGrams": 3,
          "tokenization": "edgeGram",
          "type": "autocomplete"
        }
      ]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And then in backend I created Connect.py in main folder to connect with the cluster for searching the index from views.py search action. using pymongo package built for python to connect with MongoDB, as djongo does not support aggregate function.&lt;/p&gt;

&lt;p&gt;So that once the user starts searching particular destination it will suggest them destinations automatically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fYB6L8UP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p26cu97lddozwoapa8q.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fYB6L8UP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p26cu97lddozwoapa8q.gif" alt="Demo Video to Show implementation of Atlas Search" width="680" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;views.py&amp;gt;search :&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rDAGCZ7E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awyquzi6f1upkkcxa4uz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rDAGCZ7E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awyquzi6f1upkkcxa4uz.png" alt="Search action in veiws.py" width="404" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how the function looks like:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add_result = collection.aggregate(
           [
               {
                   "$search": {
                       "autocomplete": {
                           "query": address,
                           "path": "name",
                           "fuzzy": {
                               "maxEdits": 2
                           }
                        }
                   }
               }
           ]
      )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This function build a pipeline that searches using the address parameter automatically from the collection as defined in MongoDB Atlas search index as I demoed at the top in my demo video.&lt;/p&gt;
&lt;h4&gt;
  
  
  How you will see this project
&lt;/h4&gt;
&lt;h5&gt;
  
  
  &lt;strong&gt;STEP 1:&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;Fork the project repository and clone it using:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/mr-palindrome/Explore-With-Us.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The Django skeleton is look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LYpDpaYA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3aigepbu37ztwew1hb5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LYpDpaYA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3aigepbu37ztwew1hb5.png" alt="Django Skeleton" width="252" height="704"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  &lt;strong&gt;STEP 2:&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;Install virtualenv then create a virtual environment test:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install virtualenv
virtualenv test
source test/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Install all the dependencies using:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;STEP 3:&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;Create a Django SECRET_KEY using following command in python3 terminal:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3
&amp;gt;&amp;gt;&amp;gt; from django.core.management.utils import get_random_secret_key  
&amp;gt;&amp;gt;&amp;gt; get_random_secret_key()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Create a .env file in explore_with_us folder to store all the environment and secret keys, like Django SECRET_KEY, mongoDB connection string.&lt;/p&gt;

&lt;p&gt;But, for demonstration purpose you don't have to create your own cluster, I have provided my own connection string to access all the data present in my Atlas cluster database(AtlasHackathon&amp;gt;Project_data)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GxnW_xCN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33u6496w5xufkf63kyiz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GxnW_xCN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33u6496w5xufkf63kyiz.png" alt='My cluster "AtlasHackathon"' width="880" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now store the Django SECRET_KEY and my connection string in .env, it will look like:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SECRET_KEY = &amp;lt;django SECRET_KEY&amp;gt;
HOST = mongodb+srv://user_demo:demo123@atlashackathon.nhdkl.mongodb.net/myFirstDatabase?retryWrites=true&amp;amp;w=majority
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;STEP 4:&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;Now you can see the website in your localhost:8000 using:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 manange.py runserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Submission Category:
&lt;/h3&gt;

&lt;p&gt;The category is &lt;strong&gt;"Choose Your Own Adventure"&lt;/strong&gt;. It could be "E-Commerce Creation" but this is not an e-commerce store or any product catalog. &lt;/p&gt;
&lt;h3&gt;
  
  
  Link to my Code
&lt;/h3&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--566lAguM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/mr-palindrome"&gt;
        mr-palindrome
      &lt;/a&gt; / &lt;a href="https://github.com/mr-palindrome/Explore-With-Us"&gt;
        Explore-With-Us
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
Explore With Us&lt;/h1&gt;
&lt;h2&gt;
Overview&lt;/h2&gt;
&lt;p&gt;Explore With Us is a vacation destination catalog website build with Django, Bootstrap and MongoDB Atlas
I created this website to take part in MongoDB Atlas Hackathon hosted by &lt;a href="https://dev.to/devteam/announcing-the-mongodb-atlas-hackathon-on-dev-4b6m" rel="nofollow"&gt;DEV.to&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
Tech I used:&lt;/h3&gt;
&lt;ul&gt;
    &lt;li&gt;HTML,CSS,JS and Bootstrap 4&lt;/li&gt;
    &lt;li&gt;Django&lt;/li&gt;
    &lt;li&gt;MongoDB Atlas&lt;ul&gt;
&lt;li&gt;I used MongoDB Atlas to store the database by creating a cluster on  [MongoDB](&lt;a href="https://www.mongodb.com/cloud/atlas/register" rel="nofollow"&gt;https://www.mongodb.com/cloud/atlas/register&lt;/a&gt;) &lt;/li&gt;
&lt;li&gt;I used the Atlas search feature to implement a fast, relevance-based full-text search for the destinations that I have in my database.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
How I used Atlas Search feature:&lt;/h3&gt;
&lt;p&gt;I used MongoDB Atlas search to implement the autocomplete feature in my "Search Destination" Search Bar
So that users can easily find their suitable destination within few seconds without scrolling
First I made a search index for my collection "main_destination".
&lt;br&gt;&lt;/p&gt;
&lt;p&gt;The Search Index I used:&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;
&lt;pre class="notranslate"&gt;&lt;code&gt;{
  "mappings": {
    "dynamic": false
    "fields": {
      "name": [
        {
          "foldDiacritics": false,
          "maxGrams": 7,
          "minGrams": 3,
          "tokenization":&lt;/code&gt;&lt;/pre&gt;…&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/mr-palindrome/Explore-With-Us"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;



&lt;h3&gt;
  
  
  Additional Resources / Info
&lt;/h3&gt;

&lt;p&gt;This was my first time using MongoDB as the database for a website which was a really great experience of using NoSQL specially MongoDB Atlas, I'm also planning to use it in my future projects.&lt;br&gt;
I learned about &lt;a href="https://pymongo.readthedocs.io/en/stable/"&gt;pymongo package&lt;/a&gt; and &lt;a href="https://www.djongomapper.com/"&gt;djongo&lt;/a&gt; availabe for python. I took help from &lt;a href="https://www.mongodb.com/compatibility/mongodb-and-django"&gt;How to Use Django with MongoDB&lt;/a&gt; document.&lt;br&gt;
Initially I started researching about Atlas search and how to use them using the &lt;a href="https://docs.atlas.mongodb.com/atlas-search/"&gt;Atlas Search Docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And another resource that help me to build my frontend was &lt;a href="https://getbootstrap.com/docs/4.0/getting-started/introduction/"&gt;Bootstrap 4&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>atlashackathon</category>
      <category>django</category>
      <category>mongodb</category>
    </item>
  </channel>
</rss>
