第二次请求时 django api 的问题
Problems with django api on second request
我正在开发一款用于为教师创建时间表的 flutter 应用程序。我创建了一个 Django 项目来根据用户 post 请求中的数据生成计划。这个 post 请求是从 flutter 应用程序发送的。 Django 项目不使用数据库或任何东西,它只是接收输入数据、创建计划和 returns 将输出数据返回给用户。
问题是创建计划的过程在启动 Django 服务器后仅运行 1 次。因此,当我希望另一个用户发送请求并接收计划时,我必须重新启动服务器……也许服务器会记住上一个请求的部分数据?我不知道。有什么方法可以让它在请求完成后忘记一切吗?
当我尝试在不在 Django 项目中的情况下重复 运行 调度程序时,它可以完美运行。 Scheduler 基于 Google cp_model sat 求解器。 (来自 ortools.sat.python 导入 cp_model)。我在 Django 项目中第二次 运行 调度程序时得到的错误是 'TypeError: LonelyDuoLearner_is_present_Ss9QV7qFVvXBzTe3R6lmHkMBEWn1_0 is not a boolean variable'.
是否有某种方法可以解决此问题或模仿重启服务器的效果?
django 视图如下所示:
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')
重启服务器没有什么漂亮的方法(除了强行杀掉它,这可算不上漂亮)。
您可能在未显示的代码中某处使用了一些全局状态,结果搞砸了。
你应该改为解决这个问题,或者如果你不能这样做,运行 在子流程中解决(使用例如 subprocess.check_call()
或 multiprocessing.Process()
)。
CP-SAT 求解器是无状态的。唯一的 persistent/shared 对象是 Ctrl-C 处理程序,可以使用 sat 参数禁用它。 (catch_sigint
如果我没记错的话)。
我正在开发一款用于为教师创建时间表的 flutter 应用程序。我创建了一个 Django 项目来根据用户 post 请求中的数据生成计划。这个 post 请求是从 flutter 应用程序发送的。 Django 项目不使用数据库或任何东西,它只是接收输入数据、创建计划和 returns 将输出数据返回给用户。
问题是创建计划的过程在启动 Django 服务器后仅运行 1 次。因此,当我希望另一个用户发送请求并接收计划时,我必须重新启动服务器……也许服务器会记住上一个请求的部分数据?我不知道。有什么方法可以让它在请求完成后忘记一切吗?
当我尝试在不在 Django 项目中的情况下重复 运行 调度程序时,它可以完美运行。 Scheduler 基于 Google cp_model sat 求解器。 (来自 ortools.sat.python 导入 cp_model)。我在 Django 项目中第二次 运行 调度程序时得到的错误是 'TypeError: LonelyDuoLearner_is_present_Ss9QV7qFVvXBzTe3R6lmHkMBEWn1_0 is not a boolean variable'.
是否有某种方法可以解决此问题或模仿重启服务器的效果?
django 视图如下所示:
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')
重启服务器没有什么漂亮的方法(除了强行杀掉它,这可算不上漂亮)。
您可能在未显示的代码中某处使用了一些全局状态,结果搞砸了。
你应该改为解决这个问题,或者如果你不能这样做,运行 在子流程中解决(使用例如 subprocess.check_call()
或 multiprocessing.Process()
)。
CP-SAT 求解器是无状态的。唯一的 persistent/shared 对象是 Ctrl-C 处理程序,可以使用 sat 参数禁用它。 (catch_sigint
如果我没记错的话)。