how to restart a gunicorn server without leaving vim

I’ve been using the gunicorn WSGI server lately while I work on my next project. gunicorn is fantastic, except for one tiny nuisance. I have to restart gunicorn as I change my app code. At the beginning, this meant I would switch over to the terminal where gunicorn was running. Then I’d kill the parent process by hitting CTRL+C, and then start it up again, by typing:

gunicorn crazyproject.webapp:make_application

Other frameworks often have a development mode that forks off a helper process that watches the source code folder. When some file changes, the helper tells the server to reload.

I’m not using any frameworks this time, so I don’t have that feature. I never really liked the fact that every time I saved a file, I would restart the server during development. Instead, in this case, I want to easily restart gunicorn, but only when I want to, and I don’t want to leave my editor.

It turned out to be pretty easy to do.

First I read in the excellent gunicorn docs how to tell gunicorn to reload my app:

How do I reload my application in Gunicorn?

You can gracefully reload by sending HUP signal to gunicorn:

$ kill -HUP masterpid

Next I added a –pid /tmp/gunicorn.pid option to the command that I use to start gunicorn, so that gunicorn would write the parent’s process ID into /tmp/gunicorn.pid.

Now any time I want gunicorn to reload, I can do this little command in a terminal window:

$ kill -HUP `cat /tmp/gunicorn.pid`

Those backticks around cat /tmp/gunicorn.pid tell the shell to do that part of the command first, and then feed the result into the rest of the command.

You can always use ! in vim to run some command-line program. If I had to explain the difference between vim and emacs, the one difference that is most interesting to me is that vim makes it super-easy to send buffer contents to other programs or read buffer contents from other programs, while the people behind emacs seem to think that any external dependency should ultimately be moved into emacs itself. I get the feeling that vim wants to be my text editor, but emacs wants to be my OS.

Anyhow, while I’m in vim, I run that command to restart like this:

: !kill -HUP `cat /tmp/gunicorn.pid`

After using that code for a while, and feeling confident it worked right, I mapped F12 in vim to do that action for me by adding this little thing to the end of my ~/.vimrc:

:map :!kill -HUP `cat /tmp/gunicorn.pid`

Now that means I hit F12 when I want to reload gunicorn.

Learning a new vim trick is more interesting than working on my Technology Investment Tax Credit application

The Ohio Technology Investment Tax Credit is a fantastic boon to starting businesses. But applying for it is really boring.

Anyway, when I opened up vim to work on my application, I found a really good tip of the day, copied below:

VimTip 295: Line/word/file/whatever completion

In addition to vimtip #291 you can use whole completion mode. It can complete whole lines (<C-x>l, then <C-p>, <C-n>), filenames (<C-f>), keywords, words from custom dictionary and many, many others. During coding it usually saves a LOT of key strokes 😉 This mode has many other powerful features, for example when completing word (by &;tlC-x><C-p> or just by <C-p>) you can continue completion with another <C-x<>-p>. For example, after writing such text:

this is first line
second line is here

Placing cursor at third line and pressing <C-x>l will double last line – <C-n>, <C-p> in this moment can be used to manipulate completed line. Or, instead of completing whole line you can press ‘f’ and then complete by <C-p> which will result in ‘first’ word. After that you can <C-x><C-p> to get ‘line’ word (since this is next word after ‘first’). Try yourself for other powerful combinations.

Now I just need a completion mode that will calculate intelligent sales forecasts, and this application would be done.

A few examples of how to use :g in vim

This vim tip of the day is really good. I use the :g/pattern/d command occasionally, but there is a lot more possible. Here’s the tip:


VimTip 227: Power of :g
http://vim.sourceforge.net/tip_view.php?tip_id=

:g is something very old and which is very powerful. I just wanted to illustrate the use of it
with some examples. Hope, it will be useful for someone.

Brief explanation for ":g"
-------------------------
Syntax is:
:[range]:g//[cmd]
You can think the working as, for the range (default whole file), execute
the colon command(ex) "cmd" for the lines matching . Also, for all
lines that matched the pattern, "." is set to that particular line (for
certain commands if line is not specified "." (current line) is assumed).

Some examples
-------------
Display context (5 lines) for all occurences of a pattern
:g//z#.5
:g//z#.5|echo "=========="
<< same as first, but with some beautification >>
Delete all lines matching a pattern
:g//d
Delete all blank lines (just an example for above)
:g/^\s*$/d
Double space the file
:g/^/pu =\"\n\"
:g/^/pu _
<< the above one also works >>
Copy all lines matching a pattern to end of file
:g//t$
Yank all lines matching a pattern to register 'a'
0"ay0:g//y A
Increment the number items from current line to end-of-document by one
:.,$g/^\d/exe "normal! \"
Comment (C) lines containing "DEBUG" statements
g/^\s*DEBUG/exe "norm! I/* \A */\"
A Reverse lookup for records
(eg: An address book, with Name on start-of-line and fields after a space)
:g/?^\w?p "if only name is interested
:g//ka|?^\w?p|'ap "if name and the lookup-line is interested
:g//?^\w?|+,/^[^ ]/-1p "if entire record is interested
Reverse a file (just to show the power of 'g')
:g/^/m0

Foot note 1: use :v to negate the search pattern
Foot note 2: Some explanation of commonly used commands with :g
:2,8co15 => Copy lines 2 through 8 after line 15
:4,15t$ => Copy linesa 4 through 15 towards end of document (t == co)
:-t$ => Copy previous line to end of document
:m0 => Move current line to the top of the document
:.,+3m$-1 => Move current line through cur-line+3 to the last but one line
of the document
Foot note 3: Commands used with :g are ex commands, so a help search should
be,
:help :
eg. :help :k

How to use vimdiff as the subversion diff tool

vimdiff is fantastic. Follow these instructions to make subversion use vimdiff when you run svn diff.

Get this diffwrap.sh script and save it anywhere. I saved mine in my $HOME/bin directory. Make sure to make it executable! I’m showing it below:

#!/bin/sh

# Configure your favorite diff program here.
DIFF="/usr/bin/vimdiff"

# Subversion provides the paths we need as the sixth and seventh
# parameters.
LEFT=${6}
RIGHT=${7}

# Call the diff command (change the following line to make sense for
# your merge program).
$DIFF $LEFT $RIGHT

# Return an errorcode of 0 if no differences were detected, 1 if some were.
# Any other errorcode will be treated as fatal.

Then change your $HOME/.subversion/config file to point at that script:

[helpers]
diff-cmd = /home/matt/bin/diffwrap.sh

Then go diff a file!

See this section of the svn book for all the details.