<?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: Dheeraj Jha</title>
    <description>The latest articles on DEV Community by Dheeraj Jha (@jhadheeraj1986).</description>
    <link>https://dev.to/jhadheeraj1986</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%2F293634%2Fb9b32000-5b5e-489e-b42c-7b4b1293317d.png</url>
      <title>DEV Community: Dheeraj Jha</title>
      <link>https://dev.to/jhadheeraj1986</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jhadheeraj1986"/>
    <language>en</language>
    <item>
      <title>How does A Python Program run?</title>
      <dc:creator>Dheeraj Jha</dc:creator>
      <pubDate>Thu, 02 Jul 2020 17:00:28 +0000</pubDate>
      <link>https://dev.to/jhadheeraj1986/how-does-a-python-program-run-3gg0</link>
      <guid>https://dev.to/jhadheeraj1986/how-does-a-python-program-run-3gg0</guid>
      <description>&lt;p&gt;Before jumping into how does python runs internally, let's see the difference between compiler and interpreter in brief.&lt;/p&gt;

&lt;h1&gt;
  
  
  Compiler:
&lt;/h1&gt;

&lt;p&gt;From Wikipedia:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“A compiler is a computer program that translates computer code is written in one programming language (the source language) into another language (the target language). The name compiler is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language, object code, or machine code) to create an executable program.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, a compiler is a software which will take your code written in a high-level language and do some magic and convert it to low-level language which your CPU will understand and execute. Compiled binaries are faster as compiler need not validate binaries every time. You can execute it directly. &lt;/p&gt;

&lt;p&gt;Usually, compiled binaries can not run on different operating systems, this is a drawback. Although there are ways and there are different cross compilers available which are a solution for this limitation. I will not go in detail as compile is not in the topic of this blog.&lt;/p&gt;

&lt;h1&gt;
  
  
  Interpreter:
&lt;/h1&gt;

&lt;p&gt;From Wikipedia:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“In computer science, an interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, it means interpreter will not process your code before executing. As soon as you run the program, it will start executing your code line by line, starting from line number 1. While executing your code if it gets some syntax error at any line number “n”, it will stop execution and give an error. So, the program will run till line “n-1” which is not the case with compilers. The compiler checks for the syntax and language rule and converts your code in machine language before it starts executing.&lt;/p&gt;

&lt;h1&gt;
  
  
  Python Interpreter:
&lt;/h1&gt;

&lt;p&gt;Python interpreter is a software program that executes other programs. It’s is a layer of software logic between your code and machine. It may be implemented in C, JAVA etc. depending on the Python flavour you are using.&lt;/p&gt;

&lt;p&gt;Let’s check what happens in python.&lt;/p&gt;

&lt;h1&gt;
  
  
  What happens when you write “python your_file.py"?
&lt;/h1&gt;

&lt;p&gt;You wrote a very complex logic in your python script and now going to execute it. What happens behind the scene?&lt;/p&gt;

&lt;p&gt;When you execute a program, it first compiles your complex logic code into a format known as &lt;strong&gt;Byte Code&lt;/strong&gt;. Python interpreter then processes this byte code and execute instruction one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Byte code:
&lt;/h2&gt;

&lt;p&gt;The compilation is a translation step and byte code generated in this step is a low level, platform-independent representation of your complex logic. This byte code can run faster than your code.&lt;/p&gt;

&lt;p&gt;Python stores the byte code of your program in files with extension &lt;strong&gt;.pyc (.pyc means compiled .py source)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;n python 2.x, byte code was created at the same location where your source file is but from 3.2 onwards, it is saved in a subdirectory named &lt;strong&gt;&lt;strong&gt;pycache&lt;/strong&gt;&lt;/strong&gt; located in the same directory where your complex code is and in files whose names identify the Python version which created this byte code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;test.cpython-38.pyc&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a sample byte code file name which has your source file name as a first part. The second part is the interpreter type and python version. In this example byte code is created using &lt;strong&gt;CPython&lt;/strong&gt; and &lt;strong&gt;python version&lt;/strong&gt; is 3.8(number 38 in the file name) and the third part is byte code file extension.&lt;/p&gt;

&lt;p&gt;The new &lt;strong&gt;pycache&lt;/strong&gt; subdirectory helps to avoided clutter and naming convention for byte code prevents different python versions installed from overwriting each other’s byte code.&lt;/p&gt;

&lt;p&gt;So, let’s summarize how byte codes are generated:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Python checks the last modified timestamps of source and byte code to know if code needs to recompile.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python check to see if the file needs to recompile is python version is changed, using either a “magic” version number in the byte code file itself in 3.2 and earlier or the information present in byte code filenames in 3.2 and later.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The result is that both source code changes and differing Python version numbers will trigger a new byte code file.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If python can not write the byte code files on your machine, your programs still work. The byte code is generated in memory and discarded on program exit.&lt;/p&gt;

&lt;p&gt;Python will execute your code if finds a valid byte code of your program. It does not matter if your source code is present or not.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is the stage where all syntax/indentation errors are generated.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  But it did not create a byte code for my script.
&lt;/h1&gt;

&lt;p&gt;Keep in mind that byte code is saved in files only for files that are imported, not for the top-level files of a program that are only run as scripts. it’s an &lt;strong&gt;import optimization&lt;/strong&gt; done in python.&lt;/p&gt;

&lt;p&gt;What it means is, if your script does not import any file, byte code will not be saved on machine, but it will be generated in memory.&lt;/p&gt;

&lt;p&gt;Let’s simulate this. Let's write 3 small script files.&lt;br&gt;
test1.py:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import test2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;test2.py:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import test3
print("Namaste world!!!")
test3.foo()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;test3.py:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def foo():
 if 1:
  print("Namaste world from Foo!!!")
 print("Namaste world from Foo - 1!!!")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have 3 files in our directory. Let’s run this as see what happens.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K8zRTQvc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.wixstatic.com/media/292942_c06536a8b1a742ceb09e7d58d24915d2%257Emv2.png/v1/fill/w_664%2Ch_384%2Cal_c%2Clg_1%2Cq_90/292942_c06536a8b1a742ceb09e7d58d24915d2%257Emv2.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K8zRTQvc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.wixstatic.com/media/292942_c06536a8b1a742ceb09e7d58d24915d2%257Emv2.png/v1/fill/w_664%2Ch_384%2Cal_c%2Clg_1%2Cq_90/292942_c06536a8b1a742ceb09e7d58d24915d2%257Emv2.webp" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On a successful run, it will generate a subfolder &lt;strong&gt;pycache&lt;/strong&gt; here. But what is there in this directory?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3OvC4Pfh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.wixstatic.com/media/292942_5a79f539ff424cebb125dfe7a6a3ad92%257Emv2.png/v1/fill/w_740%2Ch_167%2Cal_c%2Clg_1%2Cq_90/292942_5a79f539ff424cebb125dfe7a6a3ad92%257Emv2.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3OvC4Pfh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.wixstatic.com/media/292942_5a79f539ff424cebb125dfe7a6a3ad92%257Emv2.png/v1/fill/w_740%2Ch_167%2Cal_c%2Clg_1%2Cq_90/292942_5a79f539ff424cebb125dfe7a6a3ad92%257Emv2.webp" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see clearly, python interpreter compiled our code and generated byte code for test2.py and test3.py. No byte code is created for test1.py as it is the main script.&lt;/p&gt;

&lt;p&gt;Hope it is clear now that byte code files are saved for files which are imported in your script.&lt;/p&gt;

&lt;p&gt;But still, if you want to save a byte code file for your script. You will have to write a small script to compile your main program. Add this code in test.py. Replace your script file in place of test4.py.&lt;/p&gt;

&lt;p&gt;test.py:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import py_compile
py_compile.compile('test4.py')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;test4.py:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def foo():
 if 1:
 print("Namaste world from Foo!!!")
 print("Namaste world from Foo - 1!!!")

foo()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;when you will run this compile script, it will compile your main script and save a byte code in &lt;strong&gt;pycache&lt;/strong&gt; subdirectory. You can run this byte code file.&lt;/p&gt;

&lt;h1&gt;
  
  
  What next, I have a byte code.
&lt;/h1&gt;

&lt;p&gt;Once your program has been compiled to byte code either in a file or in memory, it is forwarded to &lt;strong&gt;Python Virtual Machine (PVM)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python virtual machine:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uGaJOEY2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.wixstatic.com/media/292942_e0f01176b5554be1ba5b2d2c5f215676%257Emv2.png/v1/fill/w_468%2Ch_153%2Cal_c%2Clg_1%2Cq_90/292942_e0f01176b5554be1ba5b2d2c5f215676%257Emv2.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uGaJOEY2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.wixstatic.com/media/292942_e0f01176b5554be1ba5b2d2c5f215676%257Emv2.png/v1/fill/w_468%2Ch_153%2Cal_c%2Clg_1%2Cq_90/292942_e0f01176b5554be1ba5b2d2c5f215676%257Emv2.webp" alt="alt text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;ref: Learning Python by Mark Lutz(5th Edition)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Python Virtual machine is a big code that iterates over your byte code and executes each instruction one by one on your machine. It’s a runtime engine which is always present as part of python. PVM is the one which runs your code on the machine. Python is designed such a way to hide all these complexities from python programmer.&lt;/p&gt;

&lt;p&gt;There are some performance implications of PVM. The PVM loop still must interpret the byte code and byte code instruction require more work than CPU instructions.&lt;/p&gt;

&lt;p&gt;The bottom line is, unlike in classic interpreter, there is still an internal compilation in python.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Runtime errors are generated in this stage.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;*Python interpreter checks for the timestamp of your script and byte code file. If there is any change in the timestamp, it will recompile the script.&lt;/p&gt;

&lt;p&gt;*It will read your script line by line and convert it to bytecode and saves as a byte code file in &lt;strong&gt;pycache&lt;/strong&gt; directory. If there are multiple files imported, it will generate byte code file for each imported file. At this stage, syntax errors are generated.&lt;/p&gt;

&lt;p&gt;*Load this byte code file in memory and send it to PVM. PVM will loop over the byte code and execute each instruction. This is the step where all Runtime errors are created in your code.&lt;/p&gt;

&lt;p&gt;In my future blog, I will dig deeper into byte code and we will play with it.&lt;/p&gt;

</description>
      <category>python</category>
      <category>computerscience</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>CPP-Community Discussion</title>
      <dc:creator>Dheeraj Jha</dc:creator>
      <pubDate>Sat, 20 Jun 2020 10:53:26 +0000</pubDate>
      <link>https://dev.to/jhadheeraj1986/cpp-community-discussion-3dh5</link>
      <guid>https://dev.to/jhadheeraj1986/cpp-community-discussion-3dh5</guid>
      <description>&lt;p&gt;We are building a strong community for C++ at "&lt;em&gt;&lt;strong&gt;CPP-Community Discussion&lt;/strong&gt;&lt;/em&gt;" forum. We are C++ developers from all experience level and believe in collaborating and learning together.&lt;/p&gt;

&lt;p&gt;We do a community session on each &lt;strong&gt;Saturday&lt;/strong&gt; in which one of our speakers gives detail information on any C++ topic.&lt;/p&gt;

&lt;p&gt;As this is just a start, we are encouraging people to speak up on any C++ basic or advance topics but we welcome to those who want to present their own work.&lt;/p&gt;

&lt;p&gt;This is a good place to show up your skills as there is no judge. If you have any issue, you can discuss in the forum. You can collaborate with others for any project you want to work.&lt;/p&gt;

&lt;p&gt;I welcome everyone to join our community and &lt;br&gt;
"&lt;strong&gt;Let's learn together&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://forms.gle/rZtcZFM7gt2Kkn9d9"&gt;Registration link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See you in the next session. &lt;/p&gt;

</description>
      <category>cpp</category>
      <category>computerscience</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Thread: Mutex sucks</title>
      <dc:creator>Dheeraj Jha</dc:creator>
      <pubDate>Thu, 05 Mar 2020 12:17:52 +0000</pubDate>
      <link>https://dev.to/jhadheeraj1986/thread-mutex-sucks-2mbi</link>
      <guid>https://dev.to/jhadheeraj1986/thread-mutex-sucks-2mbi</guid>
      <description>&lt;p&gt;We can syncronize threads with mutex, then what is the need of Condition Variable.&lt;br&gt;
Whenever we start learning multithreading and mutex, we see some examples with mutex and CV. I found most of them does not explain the need of Condition Variable, it was very confusing for me. &lt;br&gt;
Now I know why it is required, I am trying to simulate the problem. Solution is of course Condition Variable. &lt;br&gt;
Please go through this article and let me know your suggestions. Critics are most welcome.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.jhadheeraj.com/post/condition-variable-what-is-the-problem-with-mutex-alone"&gt;https://www.jhadheeraj.com/post/condition-variable-what-is-the-problem-with-mutex-alone&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How not to pass argument to a thread.</title>
      <dc:creator>Dheeraj Jha</dc:creator>
      <pubDate>Tue, 03 Mar 2020 15:03:01 +0000</pubDate>
      <link>https://dev.to/jhadheeraj1986/how-not-to-pass-argument-to-a-thread-50ol</link>
      <guid>https://dev.to/jhadheeraj1986/how-not-to-pass-argument-to-a-thread-50ol</guid>
      <description>&lt;p&gt;&lt;a href="https://www.jhadheeraj.com/post/thread-how-to-pass-arguments-to-a-thread"&gt;https://www.jhadheeraj.com/post/thread-how-to-pass-arguments-to-a-thread&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Thread: How can I create a thread?</title>
      <dc:creator>Dheeraj Jha</dc:creator>
      <pubDate>Tue, 25 Feb 2020 17:18:40 +0000</pubDate>
      <link>https://dev.to/jhadheeraj1986/thread-how-can-i-create-a-thread-c82</link>
      <guid>https://dev.to/jhadheeraj1986/thread-how-can-i-create-a-thread-c82</guid>
      <description>&lt;p&gt;&lt;a href="https://www.jhadheeraj.com/post/thread-how-can-i-create-a-thread"&gt;https://www.jhadheeraj.com/post/thread-how-can-i-create-a-thread&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is my first blog on thread. Please check it and let me know your views.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
