我一直在想如何在后台运行脚本,这样我就可以处理一个对象而无需用户等待服务器响应。
Views.py
import subprocess, sys
def allocate_request(request, event_id):
event = get_object_or_404(MeetingEvent, id=event_id)
subprocess.Popen([sys.executable, 'sorting.py', str(event_id)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return HttpResponse("Request Submitted.")
并在同一目录中 Sorting.py
import sys
class Organize:
if len(sys.argv) <= 1:
event_id = int(sys.argv[1])
event_i = MeetingEvent.objects.get(id=event_id)
问题是......
脚本本身不能单独运行,
from .models import MeetingEvent
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
非常感谢任何帮助:)