我有一个django web应用程序,它提供了一个C代码编辑器,文本被发送到另一个服务器上的另一个django应用程序,该应用程序在.c文件中编写代码,编译代码并发送回响应。
问题是,当应用程序与多个用户一起使用时,编写和编译文件会导致竞争条件问题。
下面是我发送数据的方式:
def comp(request):
encoded_data = urllib.urlencode(request.POST)
url = urllib2.urlopen('http://serverIpadress/compile/?' + encoded_data)
tml = url.read()
return HttpResponse(tml)这就是在第二个django应用程序上处理数据的方式
def compile(request):
data2=urllib.urlencode({'': request.GET.get('content','')})
request.encoding ='koi8-r'
data=request.GET.get('content','')
handle=open('/home/user/file.c','r+')
handle.write(request.GET['content'])
handle.close()
res = commands.getstatusoutput('gcc -Wall /home/user/file.c -o /home/user/file;home/user/file')
return HttpResponse(res)我已经尝试使用信号量
sem = threading.BoundedSemaphore()
sem.aquire()
writing to file
compiling
sem.release()但是这个问题仍然存在。
我一直在寻找解决方案,但我找到的唯一解决方案是关于使用事务的数据库
感谢您的帮助
发布于 2013-02-13 22:59:46
似乎您对每个请求都使用了一个文件/home/user/file.c。因此,竞争条件迫在眉睫。
有两种解决方案:
1)写入临时文件。临时文件可以从tempfile模块生成,也可以只创建随机文件名。
2)写入内存文件。您可以使用StringIO (或更快的cStringIO)模块来创建这样的文件,然后可以通过管道将其发送给gcc。
对于解决方案1),有很多方法可以做到这一点,但这是我的解决方案:
更改此部件
data=request.GET.get('content','')
handle=open('/home/user/file.c','r+')
handle.write(request.GET['content'])
handle.close()至
# you need 'random', 'string', 'os' modules imported
data=request.GET.get('content','')
filename = "".join(random.sample(string.letters, 10)) + ".c" #sample 10 letters from A-z
filepath = os.path.join('home','user','filename')
handle=open(filepath,'r+')
handle.write(request.GET['content'])
handle.close()
res = commands.getstatusoutput('gcc -Wall %s -o /home/user/file;home/user/file' %filepath)
os.remove(filepath) #remove temporary file after compiling此外,commands模块也已弃用。您应该使用subprocess.call。
发布于 2013-02-13 22:52:18
您应该为每个请求生成随机文件,并使用该文件。tempfile模块可以帮助你做到这一点。
另外,如果您不关心生成的二进制文件,那么使用-o /dev/null是个好主意。
https://stackoverflow.com/questions/14856175
复制相似问题