django server code not updating
Solution 1
If you are running a development server provided with django, namely ./manage.py runserver
you should not experience this problem, since it automatically recompiles upon .py file saving. But if you are running wsgi say with apache server it is a common issue, but with a very simple solution. Basically what happens, whenever you change a .py file, server will still use a previously compiled python file. So solution is either restart apache sudo service apache2 restart
(which is an overkill) or simply run touch wsgi.py
, wherever your wsgi.py is located within a project, which will tell it to recompile .py files. Of course you can also delete the .pyc files, but that is too tedious.
Edit:
I just notice that you are using @background
decorator, which make me believe you are using django-background-task. If that's the case, you will have to make sure this app actually takes your code changes into account too, which may not happen automatically while recompiling python files using touch wsgi.py
approach. I don't have experience with this particular app, but for example with celery, which is a lot more sophisticated app for scheduling tasks, you would also have to restart the worker in order to reflect the changes in code, since worker actually stores a pickled version of the code.
Now after checking out the django-background-task app (which seems like the one you are using), in theory you should't have to do anything special, but just to make sure you can clear out any scheduled tasks and reschedule them again. I haven't tried but you should be able to do it through ./manage.py shell
:
>>> from background_task.tasks import tasks
>>> tasks._tasks = {}
And probably you'll even have to clear out the db, for the DBTaskRunner
:
>>> from background_task.models import Task
>>> Task.objects.all().delete()
Solution 2
May be this is what you are looking for?
result = (
("item1",{'key1': 'val1'}),
("item2",{'key2': 'val2'}),
)
my_dict = {
'key1': 'updated val',
'new key': 'new value'
}
pprint.pprint(result[0]) # Result: ('item1', {'key1': 'val1'})
key, dct = result[0]
dct.update(my_dict)
pprint.pprint(result) # Result: (('item1', {'key1': 'updated val', 'new key': 'new value'}), ('item2', {'key2': 'val2'}))
Solution 3
The issue may be related to compiled python files or ".pyc" files located in your project folder on your server. These files are automatically generated as the python code is interpreted. Sometimes these files aren't re-compiled even if new code is present and it keeps running the old code.
You can install "django-extensions" via pip and it comes with a handy manage.py command that can help you clear those files:
python manage.py clean_pyc
If that doesn't work then you need to restart your wsgi server that's running the code since the python code is in memory.
Solution 4
If the code is not updating, solution will depend on how you are serving your application. If you are using ./manage.py runserver, you should not be having problems. Also if serving with apache + mod_python if you restart the sever the code should be reloaded.
But I assume you are not doing either of them, since you still have this problem. If your using uWSGI or gunicorn, they runs in a separate processes from the server, you would need to restart them separately. uWSGI can be set up also to reload the app every time its setting file changes, so normally I just call touch myapp.ini
to reload the code.
Related videos on Youtube
Ryan Saxe
Updated on August 02, 2022Comments
-
Ryan Saxe over 1 year
I have an extensive program that is running on my server. The line with the error looks like the following:
result[0].update(dictionary)
result[0]
looks like("label",{key:value,...})
so I got an error saying that atuple
does not haveupdate
when I fixed it to be
result[0][1].update(dictionary)
, I got the same error!I then added
print "test"
above to see what happened, and I got the same error, but it gave me the error occurring at the print statement. This tells me that the code the server is running is the original and not the edited one. I tried restarting the server. I have saved my code. I do not understand why nor how this is happening. What could be causing this and how can I make it so that the server recognizes the newer version?error message
Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/background_task/tasks.py", line 160, in run_task tasks.run_task(task.task_name, args, kwargs) [2/1832] File "/usr/local/lib/python2.7/dist-packages/background_task/tasks.py", line 45, in run_task task.task_function(*args, **kwargs) File "/.../proj/tasks.py", line 10, in automap_predict automap_obj.predict() File "/.../proj/models.py", line 317, in predict prediction = predictions[1] File "/.../proj/models.py", line 143, in predict #this is a recursive call File "/.../proj/models.py", line 143, in predict #this is a recursive call File "/.../proj/models.py", line 127, in predict #result[0].update(dictionary) this happens at the base case of the recursion AttributeError: 'tuple' object has no attribute 'update'
notice that I am getting this error on a line that is commented out. Displaying that this is not the code that is really running.
view
def view(request,param): run_background_task(param) return redirect("project.view.reload")
background_task
@background(schedule=0) def run_background_task(param): obj = MyModel.objects.get(field=param) obj.predict()
predict function
This is where the
result
gets created. I am not permitted to show this code, but note that I am sure that the actual code is irrelevant. It was working. I changed it to make a quick update. I got an error. I then fixed the error, but proceeded to get the same error. I even went back to the old version that was working and I still get the same error. Therefor this error has nothing to do with the contents of this function.Let me know if I can be of any more help.
-
Brandon Taylor over 9 yearsHave you tried deleting all of the
.pyc
files, if any? -
Håken Lid over 9 yearsAre you sure the file path is correct, and that you aren't working in a different virtualenv? Have you restarted the wsgi server?
-
-
Ryan Saxe over 9 yearsthis is what I was trying to do, but was not working. The issue is not with the code, it is with the server.
-
Jesuisme about 4 years+1 Touching the wsgi file was the only way I was able to solve this problem in Django somewhat elegantly. To implement touch in Python (without using a system command) see stackoverflow.com/questions/1158076/…