title: How to disable form fields and buttons until a Python subprocess has terminated?
body: <p>I'm a JavaScript/jQuery newbie and need some help with it. In a Django application, I have a single HTML page that contains two forms. When clicking the submit button of the first form, a Python subprocess gets started by the respective Django view. The fields of the first form are for passing parameters to this subprocess. The second form doesn't contain any fields. Its only purpose is to stop the same subprocess when its submit button is clicked.</p>
  
  <p>The entire form submission process happens on the server side. I want to know how to accomplish the following behavior using jQuery:</p>
  
  <ol>
  <li>When the HTML page is loaded for the first time, enable all form
  fields and buttons except for the <em>stop subprocess</em> button (since
  there is nothing to stop yet)</li>
  <li>When the <em>start subprocess</em> button is clicked, the form's fields and the button itself should be disabled until the subprocess has finished. At the same time, the <em>stop subprocess</em> button should be
  enabled.</li>
  <li>When the <em>stop subprocess</em> button is clicked, disable it again until the subprocess has really finished. When the subprocess has finished, go back to step 1.</li>
  </ol>
  
  <p>I know in general how to use jQuery to disable form elements. My problem rather is how make jQuery aware of the status of my subprocess. </p>
  
  <p>Here is the relevant code of the Django views:</p>
  
  <pre><code>def process_main_page_forms(request):
      if request.method == 'POST':
          if request.POST['form-type'] == u'webpage-crawler-form':
              template_context = _crawl_webpage(request)
  
          elif request.POST['form-type'] == u'stop-crawler-form':
              template_context = _stop_crawler(request)
      else:
          template_context = {
              'webpage_crawler_form': WebPageCrawlerForm(),
              'stop_crawler_form': StopCrawlerForm()}
  
      return render(request, 'main.html', template_context)
  
  def _crawl_webpage(request):
      webpage_crawler_form = WebPageCrawlerForm(request.POST)
  
      if webpage_crawler_form.is_valid():
          url_to_crawl = webpage_crawler_form.cleaned_data['url_to_crawl']
          maximum_pages_to_crawl = webpage_crawler_form.cleaned_data['maximum_pages_to_crawl']
  
          program = 'python manage.py crawlwebpages' + ' -n ' + str(maximum_pages_to_crawl) + ' ' + url_to_crawl
          p = subprocess.Popen(program.split())
  
      template_context = {
          'webpage_crawler_form': webpage_crawler_form,
          'stop_crawler_form': StopCrawlerForm()}
  
      return template_context
  
  def _stop_crawler(request):
      stop_crawler_form = StopCrawlerForm(request.POST)
  
      if stop_crawler_form.is_valid():
          with open('scrapy_crawler_process.pid', 'rb') as pidfile:
              process_id = int(pidfile.read().strip())
  
          # These are the essential lines
          os.kill(process_id, signal.SIGTERM)
          while True:
              try:
                  time.sleep(10)
                  os.kill(process_id, 0)
              except OSError:
                  break
          print 'Crawler process terminated!'
  
      template_context = {
          'webpage_crawler_form': WebPageCrawlerForm(),
          'stop_crawler_form': stop_crawler_form}
  
      return template_context
  </code></pre>
  
  <p>Thank you very much in advance!</p>