Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django server code not updating

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 a tuple does not have update

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.

like image 832
Ryan Saxe Avatar asked Jan 16 '15 18:01

Ryan Saxe


4 Answers

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()
like image 77
lehins Avatar answered Sep 26 '22 03:09

lehins


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'}))
like image 27
Du D. Avatar answered Sep 22 '22 03:09

Du D.


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.

like image 35
Alex Carlos Avatar answered Sep 25 '22 03:09

Alex Carlos


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.

like image 26
Michael Buckley Avatar answered Sep 25 '22 03:09

Michael Buckley