{"id":4362,"date":"2017-08-22T08:24:14","date_gmt":"2017-08-22T08:24:14","guid":{"rendered":"https:\/\/islascruz.org\/blog\/?p=4362"},"modified":"2017-08-24T04:02:41","modified_gmt":"2017-08-24T04:02:41","slug":"quick-tip-vagrantdjango","status":"publish","type":"post","link":"https:\/\/islascruz.org\/blog\/2017\/08\/22\/quick-tip-vagrantdjango\/","title":{"rendered":"Quick tip when using vagrant+django"},"content":{"rendered":"<p>I started to use vagrant to hold my development environment, it helps to keep the development environment isolated, since the app will run on a Linux server, with a specific database engine and probably some other specific modules, I don&#8217;t want to clutter my OS with all that stuff. More if I plan to work on another project that maybe, have a dependency on other versions of the same base (legacy Django perhaps?)<\/p>\n<p><!--more--><\/p>\n<p>So, Django&#8230; Django have a nice mechanism that helps development, you don&#8217;t need a running web server to try your project. No need to have Apache\/NGINX for that, and better yet, every time you update a python file it reloads the module so you always have reflected your changes.<\/p>\n<p>But there is a catch, Django (by default, or by itself) always polls every file in the project to know which file has changed, with small projects there is no problem, it&#8217;s quite fast, but when you have a bunch of files you can spend a considerable amount of CPU\u00a0 (around 6% ) just polling. Not nice, and is worst if you are in a VM (vagrant).<\/p>\n<p>A better way to do this is using <a href=\"https:\/\/github.com\/seb-m\/pyinotify\" target=\"_blank\" rel=\"noopener\">py-inotify<\/a>, with this Django\u00a0will not\u00a0be polling, just wait until the watcher notifies about a file change, then it reloads. So, using py-inotify reduces the CPU time spent just in polling.<\/p>\n<p>But&#8230; Vagrant shares the source code folder (and other folder if you set them up) with the VM via NFS. NFS was implemented like 30 years ago and they sure didn&#8217;t took in mind inotify (it didn&#8217;t exist at that time), so, if you update the code NFS will not set the notification in the kernel, and py-inotify will never knew there was a change, so Django will not reload ?.<\/p>\n<h3>How to fix it?<\/h3>\n<p>Turns out its pretty simple, just install a vagrant plugin to forward the notification via vagrant.<\/p>\n<pre>vagrant plugin install vagrant-notify-forwarder<\/pre>\n<p>Then <strong>shutdown<\/strong> and <strong>up<\/strong> your VM in order to get the plugin and forwarder service started, and it&#8217;s ready.<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_4362\" class=\"pvc_stats all  \" data-element-id=\"4362\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/islascruz.org\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I started to use vagrant to hold my development environment, it helps to keep the development environment isolated, since the app will run on a Linux server, with a specific database engine and probably some other specific modules, I don&#8217;t want to clutter my OS with all that stuff. More if I plan to work [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_4362\" class=\"pvc_stats all  \" data-element-id=\"4362\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/islascruz.org\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":4367,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,239],"tags":[],"class_list":["post-4362","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-tips-and-tricks"],"a3_pvc":{"activated":true,"total_views":10106,"today_views":0},"brizy_media":[],"_links":{"self":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/4362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/comments?post=4362"}],"version-history":[{"count":6,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/4362\/revisions"}],"predecessor-version":[{"id":4414,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/4362\/revisions\/4414"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/media\/4367"}],"wp:attachment":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/media?parent=4362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/categories?post=4362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/tags?post=4362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}