简体   繁体   中英

Python subprocess blocking and getting BadStatusLine exception

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM