1

I am designing Django application in which client will send the multiple POST request with some data values.I have designed the function to process this data values. The structure of my view.py is something like below,

from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from update_database import update_database
import time

@csrf_exempt
def index(request):
        if request.body:
                update_database(get_username(),request.body)

        addStatusPage = loader.get_template('add_status.html')
        return HttpResponse(addStatusPage.render(RequestContext(request)))

def get_username():
        username = "james"
        return(username)

And the structure of the process_data function is like below,

import ast

def update_database(username,request_dict):
        user_status_dict = {}
        user_status_dict_key = {}
        user_status_dict_val = []

        request_dict = ast.literal_eval(request_dict)
        user_status_dict_key = (username,)
        user_status_dict_key += (request_dict["date"],)
        user_status_dict_val = [request_dict["date"],request_dict["dept"],request_dict["release"]]
        user_status_dict[user_status_dict_key] =  user_status_dict_val
        print user_status_dict

Also see the output:

{('james', '17/07/2016'): ['17/07/2016', 'OneCell', '1']}
{('james', '17/07/2016'): ['17/07/2016', 'OneCell', '1']}
{('james', '17/07/2016'){('james', '17/07/2016'): ['17/07/2016', : 'OneCell'[, '17/07/2016', 'OneCell''1', ]'1'}]
}

Sometimes when huge amount of POST request arrives then the value of the "user_status_dict" is appended with the some of the field of the old "user_status_dict". I think that is because of the race condition. Please hele me.

space earth
  • 407
  • 4
  • 18
  • This http://stackoverflow.com/questions/1030270/race-conditions-in-django does not help? – ettanany May 16 '17 at 10:21
  • I have already tried out but it is not working. – space earth May 16 '17 at 10:22
  • 2
    We can't help you solve a race condition if you don't show us the actual code that introduces the race condition. Please update your question with the relevant code. – knbk May 16 '17 at 10:40
  • @knbk,I have added actual code – space earth May 16 '17 at 10:48
  • 1
    `user_status_dict` is a local variable within `update_database()`. There is no way to introduce a race condition on a local variable, because it only exists within the context of a single function call/single thread. – knbk May 16 '17 at 10:56
  • That's true.But you can see the new dictionary is appended with old one in the output.I have pasted output in question.Please go through it. – space earth May 16 '17 at 11:02
  • 1
    Where does the output go? Are you using multiple processes? From the output I'd guess that multiple processes are writing to the same file simultaneously, which causes the output to become interleaved. I don't think that's possible in Python with multiple threads in a single process. – knbk May 16 '17 at 11:11
  • No,I am sending multiple post requests.There is only single thread.I think it is not raise condition.But since the print is taking place simultaneously that why one print statement is appearing with another one. – space earth May 16 '17 at 11:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144342/discussion-between-knbk-and-space-earth). – knbk May 16 '17 at 11:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144409/discussion-between-space-earth-and-knbk). – space earth May 17 '17 at 05:41

2 Answers2

1

The problem with the print statement.The print statement is not thread safe in the python.So it may happen that many thread can print on stdout simultaneously.I have used the thread safe print as suggested in Python 2.7: Print thread safe. Below is the update_database() function with the custom thread safe print.

from __future__ import print_function
import ast
import sys

print = lambda x: sys.stdout.write("%s\n" % x)

def update_database(username,request_dict):
        user_status_dict = {}
        user_status_dict_key = ()
        user_status_dict_val = []

        request_dict = ast.literal_eval(request_dict)
        user_status_dict_key = (username,)
        user_status_dict_key += (request_dict["date"],)
        user_status_dict_val = [request_dict["date"],request_dict["dept"],request_dict["release"]]
        user_status_dict[user_status_dict_key] =  user_status_dict_val
        print(user_status_dict)

Thanks to knbk for providing me great support.

Community
  • 1
  • 1
space earth
  • 407
  • 4
  • 18
0

where is your dict defined? if the dict is a instance of Models,you don't have to worry about the race-condition because the database will conduct the thread-safe Besides django provide a interface in wsgi,you can do your High concurrency things by gevent,uwsgi,fastcgi...