Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 文件上的django web应用程序争用条件_Python_Django_Multithreading_Apache_Race Condition - Fatal编程技术网

Python 文件上的django web应用程序争用条件

Python 文件上的django web应用程序争用条件,python,django,multithreading,apache,race-condition,Python,Django,Multithreading,Apache,Race Condition,我有一个django web应用程序,它提供了一个C代码编辑器,文本被发送到另一台服务器上托管的另一个django应用程序,该应用程序将代码写入.C文件,然后编译并发送回响应 当应用程序与多个用户一起使用时,写入和编译文件会导致竞争条件问题 以下是我发送数据的方式: def comp(request): encoded_data = urllib.urlencode(request.POST) url = urllib2.urlopen('http://serverIpadress

我有一个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()
但问题依然存在

我一直在寻找解决方案,但我发现的唯一解决方案是关于使用事务的数据库


感谢您的帮助

您应该为每个请求生成随机文件并使用它。该模块可以帮助您实现这一点


另外,如果您不关心生成的二进制文件,那么使用
-o/dev/null
是个好主意。

对于每个请求,您似乎都使用单个文件
/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()


另外,
命令
模块也不推荐使用。您应该使用
子流程。call

非常感谢您的解决方案我也会尝试一下,假设request.GET是一个类似dict的对象,为什么要执行两次“GET”?通过
data=request.GET.GET('content','')
可以将GET值安全地分配到
data
,并且
data
永远不会被使用。检查:
handle.write(request.GET['content'])
# 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