简体   繁体   中英

Problems with django api on second request

I am working on a flutter app for creating schedules for teachers. I created a Django project to generate a schedule based on the data in the post request from the user. This post request is sent from the flutter app. The Django project doesn't use a database or anything, It simply receives the input data, creates the schedule and returns the output data back to the user.

The problem is that the process of creating the schedule only works 1 time after starting the Django server. So when I want another user to send a request and receive a schedule I have to restart the server... Maybe the server remembers part of the data from the previous request?? I don't know. Is there somekind of way to make it forget everything after a request is done?

When I try to repeatedly run the scheduler without being in a Django project it works flawlessly. The Scheduler is based on the Google cp_model sat solver. (from ortools.sat.python import cp_model). The error I get when running the scheduler the second time in a Django project is 'TypeError: LonelyDuoLearner_is_present_Ss9QV7qFVvXBzTe3R6lmHkMBEWn1_0 is not a boolean variable'.

Is there some kind of way to fix this or mimic the effect of restarting the server?

The django view looks like this:

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from .scheduler.planning import Planning
from .scheduler.planning import print_json
import json

# Convert the data and creates the schedule.
@csrf_exempt
def generate_schedule(request):
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            planning = Planning()
            planning.from_json(data)
            output_json = planning.output_to_json()
            print_json(output_json)
            response = json.dumps(output_json)

        except Exception as e:
            print(e)
            print("The data provided doesn't have the right structure.")
            response = json.dumps([{'Error': "The data provided doesn't have the right structure."}])

    else:
        response = json.dumps([{'Error': 'Nothing to see here, please leave.'}])
    return HttpResponse(response, content_type='text/json')

There is no beautiful way to restart the server (aside from just killing it by force, which is hardly beautiful).

You're probably using some global state somewhere in the code you're not showing, and it gets screwed up.

You should fix that instead, or if you can't do so, run the solving in a subprocess (using eg subprocess.check_call() , or multiprocessing.Process() ).

The CP-SAT solver is stateless. The only persistent/shared object is the Ctrl-C handler, which can be disabled with a sat parameter. ( catch_sigint if my memory is correct).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM