1

I have a Django web application running on a server (apache), and I'm trying to compile a Java file from the Python code using this:

def comp(request):
   p = subprocess.Popen(['javac',filepath],stdout=subprocess.PIPE,stderr=subprocess.PIPE, bufsize=100)
      res, err =p.communicate()

   return HttpResponse (err)

When calling the comp method I get BadStatusLine exception. I googled about it and I found that the subprocess might be blocking when trying to reed the outputs... so I tried this code:

def comp(request):
   p = subprocess.Popen(['/usr/bin/javac',filepath],stdout=subprocess.PIPE,stderr=subprocess.PIPE, bufsize=100)
   while p.returncode == None:
      res, err =p.communicate()
   if p.returncode != 0:
      err = "whatever"
   return HttpResponse (err) 

it's still blocking

I've tried to write the first code directly to the python interpreter and it worked fine the results are caught successfully from the outputs I guess that the subprocess block have something to do with apache maybe... honestly i don't know what to do next I've tired many methods present in the python doc http://docs.python.org/2/library/subprocess.html but none worked

Thank you for your help.

eightShirt
  • 1,457
  • 2
  • 15
  • 29
  • I would highly recommend now calling subprocess via a http request, it has the potential to bite you in the ***. Instead using something like celery http://www.celeryproject.org/ and run it as a separate process. – Matt Seymour Mar 28 '13 at 13:23
  • This link talks about the BadStatusLine a little more http://stackoverflow.com/questions/8734617/python-django-badstatusline-error – Matt Seymour Mar 28 '13 at 13:24
  • about celery.., I should use celery from the client side to call this method right? – user2099860 Mar 28 '13 at 14:06
  • If integrating a new library isn't your thing, try using python `commands` bundle, I like that better than using subprocess: http://docs.python.org/2/library/commands.html – themanatuf Mar 28 '13 at 17:01
  • thank you themanatuf but the first time i tried to run javac command i used the command.getstatusoutput(....) but it failed with the same error so i started searching how redirect output and finally to find a way how to execute javac asynchronously – user2099860 Mar 28 '13 at 17:48
  • regarding Celery i added the module successfully and the celery deamon is running so i changed my code as follows: @task() def compile(request): "same as first code example" but nothing changed and the celery log doesn't give any info just it's running well – user2099860 Mar 28 '13 at 18:01

1 Answers1

0

I've successful installed Celery and I'm using Redis as messaging queue ,I'm able to run the tasks and get the output from any subprocess i launch except for javac the output is always an empty string here is the task code :

class JavaSubTask(Task):

   def run(self, filepath, **kwargs):
      p = subprocess.Popen(["/usr/bin/javac",filepath],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
      p.wait()
      command_output, err = p.communicate()
 return(command_output + err)
tasks.register(JavaSubTask)

I'm running celery with

python manage.py celeryd worker -l INFO

I've tried pexpect module too but gave the same result (and also commands.getstatusoutput too) I've also tried to redirect the output with 2>&1 but also failed

Thanks