0

I have been figuring on how to run a script on background, so that i can process an object without having the user to wait for the server to respond.

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.")

and in the same directory, 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)

Problem is...

the script itself can't be run alone,

from .models import MeetingEvent
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package

Would appreciate any help :)

VincentW
  • 29
  • 4
  • Is there any kind of error? Or the script Sorting.py just did not execute? – Piyush Das Nov 24 '17 at 11:45
  • @PiyushDas I've made edits to clarify my question. Hope you could provide some insights on it. – VincentW Nov 24 '17 at 14:57
  • 1
    Possible duplicate of [Executing Python script from Django shell](https://stackoverflow.com/questions/16853649/executing-python-script-from-django-shell) – Nils Werner Nov 24 '17 at 15:12

0 Answers0