Created
December 30, 2015 04:55
-
-
Save junhua/2310032d9b027f2ddf8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Not all imports are used | |
from __future__ import absolute_import | |
from celery import shared_task, current_task | |
from apps.common.util import kill_process | |
from django.conf import settings | |
from django.core.files.base import ContentFile | |
from django.core.files.storage import default_storage | |
import subprocess as sub | |
import os | |
def java_file(username, title, args, display_execution_time=False): | |
execution_time = None | |
if display_execution_time: | |
command = [ | |
'time', | |
'-p', | |
'java', | |
'-classpath', | |
# str('%s/%s' % (settings.MEDIA_ROOT, path)) | |
str('%s/submissions/%s' % (settings.MEDIA_ROOT, username)), | |
title | |
] | |
else: | |
command = [ | |
'java', | |
'-classpath', | |
# str('%s/%s' % (settings.MEDIA_ROOT, path)) | |
str('%s/submissions/%s' % (settings.MEDIA_ROOT, username)), | |
title | |
] | |
if args and len(args) > 0: | |
command = command + args | |
result = sub.Popen( | |
command, stdout=sub.PIPE, stderr=sub.STDOUT).stdout.read() | |
# Run the java command in another thread which will be killed after the timeout | |
result = kill_process.RunCmd(command, 60).Run().stdout.read() | |
# result is none when timeout occurs | |
if result is None: | |
return "Time out", execution_time | |
if display_execution_time: | |
start_index = result.rfind('real') | |
end_index = result.rfind('user') | |
# 4 is length of real, 9 is the number of spaces | |
# e.g. output ---- '<output values> 0.21 real 0.10 user 0.03 sys\n' | |
execution_time = result[start_index+4:end_index-1].strip(' \t\n\r') | |
#result_index = result[:start_index].rfind(' ') | |
result = result[0:start_index-1] | |
return result, execution_time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment