1

How does Django and/or my webserver keep straight which files it should be using when I switch Git branches?

My understanding is that when I checkout a branch in Git that the actual file system is modified. Is this true? If yes, how does Django and the webserver keep the files in tact that existed when I started the server?

For Example:


Create views.py on master branch

return HttpResponse("<h1>Hello world!</h1>")

Start server (using daphne):

daphne -b 0.0.0.0 -p 9000 my_app.asgi:application

my app output:

Hello world!

Make modifications in views.py (development branch)

return HttpResponse("<h1>Thanks for Visting!</h1>")

Page refresh

Output does not change:

Hello world!

Bring down server

Ctrl + C

Start server (using daphne):

daphne -b 0.0.0.0 -p 9000 my_app.asgi:application

New Output

Thanks for Visting!


I understand hot reloading is a feature, but how does Django and the web server avoid using the modified files on the new branch?

Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
  • Did you change the file at the server side, or only in the repository? – Willem Van Onsem Jul 03 '18 at 21:16
  • What do you mean 'at the server side'? I'm relatively new to web app deployment. I'm guessing I should not be running the server directly from the Git repo? – Scott Skiles Jul 03 '18 at 21:17
  • Well typically a repository is used to *deploy* code to the server. So you check in code at some other machine, and then schedule a deploy that will checkout the change later. – Willem Van Onsem Jul 03 '18 at 21:19
  • Okay. That is what I'm working towards. I have two servers. One is production. But I was getting more into Git branching and I ran into the scenario where I was on a hotfix Git branch on the production server. I made the change, created a diff, but was curious why the underlying filesystem did not cause the website to change. Is there a mechanism where Django "freezes" what it sees when the server gets started? Or is it collected somewhere? I've been meaning to dig into fabric also. Sorry for the essay. – Scott Skiles Jul 03 '18 at 21:25
  • well if you make a commit, that commit is typically *not* pulled at the other server, unless you instruct the machine to do that. So probably by restarting your server, you triggered a `git pull`. – Willem Van Onsem Jul 03 '18 at 21:30
  • Right. But If I bring down the server with "Ctrl + C", I am then on the "hotfix" branch, correct? – Scott Skiles Jul 03 '18 at 21:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174296/discussion-between-scott-skiles-and-willem-van-onsem). – Scott Skiles Jul 03 '18 at 21:31

1 Answers1

2

The issue here is that your views.py file is only being read once, the first time the module is imported. After that, Python won't look at that file on disk anymore, so your changes won't have any effect.

More generally, any web server should have a well-defined way of updating its content. Unless it's been explicitly designed to update itself when the underlying file system changes, you shouldn't expect it to work in that situation.

What will happen with Python, specifically, is that the code will be loaded from the filesystem once, when the module is imported, and then not touched again. So if you update the filesystem while the Python process is running, modules that have already been imported will be using the old code while newly imported modules will use the new code. (For more detail on Python's caching of modules, see this question.)

For development purposes it's common to use a web server that automatically restarts itself when it detects changes to the filesystem. That's what Django's development server does. However, it's hard to do perfectly, since the web server can't necessarily figure out every file that the code might depend on. It's also not always appropriate to simply restart, since that will close existing TCP connections.

Because of such complexities, production deployment usually works differently. It could involve an explicit shut down and restart, or might involve spinning up a new server with the new code while waiting for the connections on the old server to be closed. In any case, it will depend on the specific, documented way that the web server is meant to be used.

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102