[英]Google App Engine: “Cannot create a file when that file already exists”
I'm running the Google App Engine devserver 1.3.3 on Windows 7. 我正在Windows 7上运行Google App Engine devserver 1.3.3。
Usually, this method works fine, but this time it gave an error: 通常,此方法可以正常工作,但是这次却出现了错误:
def _deleteType(type):
results = type.all().fetch(1000)
while results:
db.delete(results)
results = type.all().fetch(1000)
The error: 错误:
File "src\modelutils.py", line 38, in _deleteType
db.delete(results)
File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1302, in delete
datastore.Delete(keys, rpc=rpc)
File "C:\Program Files\Google\google_appengine\google\appengine\api\datastore.py", line 386, in Delete
'datastore_v3', 'Delete', req, datastore_pb.DeleteResponse(), rpc)
File "C:\Program Files\Google\google_appengine\google\appengine\api\datastore.py", line 186, in _MakeSyncCall
rpc.check_success()
File "C:\Program Files\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 474, in check_success
self.__rpc.CheckSuccess()
File "C:\Program Files\Google\google_appengine\google\appengine\api\apiproxy_rpc.py", line 149, in _WaitImpl
self.request, self.response)
File "C:\Program Files\Google\google_appengine\google\appengine\api\datastore_file_stub.py", line 667, in MakeSyncCall
response)
File "C:\Program Files\Google\google_appengine\google\appengine\api\apiproxy_stub.py", line 80, in MakeSyncCall
method(request, response)
File "C:\Program Files\Google\google_appengine\google\appengine\api\datastore_file_stub.py", line 775, in _Dynamic_Delete
self.__WriteDatastore()
File "C:\Program Files\Google\google_appengine\google\appengine\api\datastore_file_stub.py", line 610, in __WriteDatastore
self.__WritePickled(encoded, self.__datastore_file)
File "C:\Program Files\Google\google_appengine\google\appengine\api\datastore_file_stub.py", line 656, in __WritePickled
os.rename(tmpfile.name, filename)
WindowsError: [Error 183] Cannot create a file when that file already exists
What am I doing wrong? 我究竟做错了什么? How could this have failed this time, but usually it doesn't?
这次怎么可能失败了,但通常不会失败?
UPDATE I restarted the devserver, and when it came back online, the datastore was empty. 更新我重新启动了devserver,当它重新联机时,数据存储为空。
Unfortunately, 1.3.3 is too far back for me to look at its sources and try to diagnose your problem precisely - the SDK has no 1.3.3 release tag and I can't guess which revision of the datastore_filestub.py
was in 1.3.3. 不幸的是,对于我而言,1.3.3太遥远了,无法查看其源代码并尝试准确地诊断您的问题-SDK没有1.3.3发行标签,而且我无法猜到
datastore_filestub.py
哪个版本在1.3中。 3。 Can you upgrade to the current version, 1.3.5, and try again? 您可以升级到当前版本1.3.5,然后重试吗? Running old versions (especially 2+ versions back) is not recommended since they'll be possibly a little out of sync with what's actually available on Google's actual servers, anyway (and/or have bugs that are fixed in later versions).
不建议运行旧版本(尤其是2个以上的版本),因为它们可能会与Google实际服务器上实际可用的版本有点不同步(和/或存在在更高版本中已修复的错误)。 Anyway...
无论如何...
On Windows, os.rename
doesn't work if the destination exists -- but the revisions I see are careful to catch the OSError
that results ( WindowsError
derives from it), remove the existing file, and try renaming again. 在Windows上,如果目标存在,则
os.rename
不起作用-但我看到的修订很小心地捕获导致的OSError
( WindowsError
源自此),删除现有文件,然后尝试重命名。 So I don't know what could explain your bug -- if the sources of the SDK you're running have that careful arrangement, and I think they do. 因此,我不知道是什么原因可以解释您的错误-如果您所运行的SDK的源经过精心安排,我认为它们确实可以。
Plus, I'd recommend to --use_sqlite
(see Nick Johnson's blog announcing it here ) in lieu of the file-stub for your SDK datastore - it just seems to make more sense!-) 另外,我建议
--use_sqlite
(请参阅Nick Johnson的博客在此处宣布)代替SDK数据存储区的文件存根-似乎更有意义!
(disclaimer: i'm not answering your question but helping you optimize the code you're running) (免责声明:我不是在回答您的问题,而是帮助您优化正在运行的代码)
your code seems to be massively deleting objects. 您的代码似乎正在大量删除对象。 in the SDK/dev server, you can accomplish wiping out the datastore using this command as a quicker and more convenient alternative:
在SDK / dev服务器中,您可以使用此命令完成擦除数据存储的操作,这是一种更快,更方便的替代方法:
$ dev_appserver.py -c helloworld
now, that is, if you want to wipe your entire SDK datastore. 现在,也就是说,如果您要擦除整个SDK数据存储区。 if not, then of course, don't use it.
如果没有,那么当然不要使用它。 :-)
:-)
more importantly, you can make your code run faster and use less CPU on production if you change your query to be something like: 更重要的是,如果将查询更改为以下内容,则可以使代码运行更快,并在生产中使用更少的CPU:
results = type.all(keys_only=True).fetch(SIZE)
this works the same as your's except it only fetches the keys as you don't need full entities retrieved from the datastore in order to delete them. 这与您的密钥的工作原理相同,只是它只获取密钥,因为您不需要从数据存储中检索完整实体来删除它们。 also, your code is currently setting
SIZE=1000
, but you can make it larger than that, esp. 同样,您的代码当前设置为
SIZE=1000
,但是您可以使其大于该值,尤其是。 if you have an idea of how many entities you have in your system... the 1000 result limit was lifted in 1.3.1 http://bit.ly/ahoLQp 如果您对系统中有多少个实体有所了解... 1000结果限制已在1.3.1 http://bit.ly/ahoLQp中取消
one minor nit... try not to use type
as a variable name... that's one of the most import objects and built-in/factory functions in Python. 一个小技巧...尽量不要使用
type
作为变量名...这是Python中最重要的导入对象和内置/工厂函数之一。 your code may act odd if do this -- in your case, it's only fractionally better since you're inside a function/method, but that's not going to be true as a global variable. 如果这样做,您的代码可能会表现得很奇怪-在您的情况下,由于您位于函数/方法内部,因此它的性能仅会好一些,但是作为全局变量,这并不正确。
hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.