为什么原子操作被认为是线程安全的?(Why are atomic operations considered thread-safe?)
原子操作如何使线程安全? 我已阅读了维基百科有关线程安全的文章 。 但文章并没有真正解释幕后的过程。 换句话说,为什么线程A执行的“原子”操作不能被线程B中断?
How are atomic operations made thread-safe? I've read about the subject in Wikipedia's article on thread-safety. But the article didn't really explain the process behind the scenes. In other words, why can't an "atomic" operation executed by a thread A be interrupted by a thread B?
原文:https://stackoverflow.com/questions/14370575
最满意答案
这里有一些巧妙的技巧。 考虑这个增强模型:
class Person(db.Model): first_name = db.StringProperty() last_name = db.StringProperty() middle_name = db.StringProperty() names_lower = db.StringListProperty()
您需要让names_lower与真实字段保持同步,例如:
p.names_lower = [p.first_name.lower(), p.last_name.lower(), p.middle_name.lower()]
您可以使用DerivedProperty更优雅地完成此操作 。
现在,您的查询:
term = self.request.get('term').lower() query = Person.all() query.filter('names_lower >=', term) query.filter('names_lower <=', unicode(term) + u"\ufffd")
这给你:
- 使用一个索引匹配所有3个属性
- 不区分大小写的匹配项
- 通配符后缀匹配
因此,对“smi”的查询将返回任何名称以“smi”开头的人。
将较小的名称复制到ListProperty可启用不区分大小写的匹配,并允许我们使用一个查询搜索所有3个字段。 “\ ufffd”是最高可能的unicode字符,因此它是我们子字符串匹配的上限。 如果由于某种原因您想要完全匹配,请过滤
'names_lower =', term
而不是'names_lower =', term
。编辑 :
我应该如何在我的数据存储区中搜索相同的内容(因为我需要查看每个字段,可能还有3个字段的组合值)? 如何优化这样的查询?
这已经在原始解决方案中得到了解决。 通过获取3个字段并将它们复制到单个ListProperty,我们实际上创建了一个单个索引,每个人有多个条目。 如果我们有一个名叫Bob J Smith的人,我的索引中会有3个点击:
- names_lower = bob
- names_lower = j
- names_lower =史密斯
这消除了在每个字段上运行不同查询的需要。
我也不清楚答复格式应该是什么。
仔细阅读文档 。 格式化jQuery的输出应该非常简单。 您的数据源将是指定URL的字符串,您需要将响应格式化为JSON。
There are a few neat tricks here. Consider this augmented model:
class Person(db.Model): first_name = db.StringProperty() last_name = db.StringProperty() middle_name = db.StringProperty() names_lower = db.StringListProperty()
You'll need to keep names_lower in sync with the real fields, e.g.:
p.names_lower = [p.first_name.lower(), p.last_name.lower(), p.middle_name.lower()]
You can do this more elegantly with a DerivedProperty.
And now, your query:
term = self.request.get('term').lower() query = Person.all() query.filter('names_lower >=', term) query.filter('names_lower <=', unicode(term) + u"\ufffd")
This gives you:
- Matching on all 3 properties with one index
- Case insensitive matches
- Wildcard suffix matches
So a query for "smi" will return any person with any name starting with "smi" in any case.
Copying lower-cased names to a ListProperty enables case-insensitive matching, and allows us to search all 3 fields with one query. "\ufffd" is the highest possible unicode character, so it's the upper limit for our substring match. If for some reason you want an exact match, filter for
'names_lower =', term
instead.Edit:
How should I search for the same in my datastore (since I need to look at each field and probably for combined value of 3 fields)? How to optimize such query?
This is already accounted for in the original solution. By taking the 3 fields and copying them to a single ListProperty, we're essentially creating a single index with multiple entries per person. If we have a person named Bob J Smith, he'll have 3 hits in our index:
- names_lower = bob
- names_lower = j
- names_lower = smith
This eliminates the need to run distinct queries on each field.
I am also not clear what should be the reply format.
Read the docs carefully. Formatting output for jQuery should be pretty straightforward. Your data source will be a string specifying a URL, and you'll want to format the response as JSON.
相关问答
更多-
GAE - 如何创建仅由应用程序使用的私有URL?(GAE - how to create a private url that is only used by the application?)[2022-08-12]
您只需要登录您的私人页面和AJAX处理程序,并将登录级别设置为admin 。 您可能需要登录一个或多个网址。 编辑 设置login: required ,这将要求用户通过他们的Google帐户登录,但仍然所有拥有Google帐户的用户都可以访问它。 然后在AJAX处理程序中检查用户是否是您授予访问权限的用户之一: https : //developers.google.com/appengine/docs/python/users/ You can simply require a login for yo ... -
我已经编写了下面的代码来处理这个问题。 基本上,我将所有可能的单词“starts”保存在列表中而不是整个句子中。 这就是这个网站的jquery自动完成的工作方式。 import unicodedata import re splitter = re.compile(r'[\s|\-|\)|\(|/]+') def remove_accents(text): nkfd_form = unicodedata.normalize('NFKD', unicode(text)) return u" ...
-
GAE - 创建应用程序问题(GAE - creating an app issue)[2023-09-30]
使用https代理服务器解决了此问题。 谢谢! Solved this issue with https proxy server. Thanks! -
GAE上传到数据库 - 或者 - 如何在GAE中创建大字典?(GAE uploading to database - or - how to create big dictionary in GAE?)[2022-06-19]
可以将批量加载程序用于您的任务。 或者只是创建自己的方法,与外部cron脚本交谈以填充新数据 It might be possible to use the bulk loader for your task. Or simply create your own method that you talk to from your external cron script for populating the new data -
这里有一些巧妙的技巧。 考虑这个增强模型: class Person(db.Model): first_name = db.StringProperty() last_name = db.StringProperty() middle_name = db.StringProperty() names_lower = db.StringListProperty() 您需要让names_lower与真实字段保持同步,例如: p.names_lower = [p.first_name.lower ...
-
假设你在实体上有一个DateProperty指示选举结束的时间,你可以每晚进行超过3个月的cron求职并删除它们。 Assuming you have a DateProperty on the entities indicating when the election ended, you can have a cron job search for any older than 3 months every night and delete them.
-
您不需要GAE实例来创建索引。 请参阅您在“部署或删除索引”中链接的文档中的部分。 您需要创建一个index.yaml文件(请参阅此页面中的示例),然后使用gcloud datastore create-indexes命令进行部署。 You don't need a GAE instance to create an index. See the section in the doc you linked on "Deploying or deleting indexes". You'll need to ...
-
调试后我发现问题出在这一行: List
dbList = (List ) q.getResultList(); if(dbList == null) addHistoryDeviceJPAToDs(historyDeviceJPAList); 'dbList'永远不会为null,如果数据存储区中没有任何内容,则它的大小为0。 这就是为什么addHistoryDeviceJPAToDs方法永远不会被 ... -
在此上下文中爆炸意味着“未压缩”。 这与GAE SDK一样正常。 只有在写入一些数据后才会创建local_db.bin文件。 Exploded in this context means "not compressed". This is normal with the GAE SDK. It will create the local_db.bin file only after you write some data.
-
如果您的第一个实例没有超载,GAE将不会创建新的实例。 每个实例可以同时处理不少请求 - 尤其是如果除了保持实例以外不做其他任何操作。 GAE will not create a new instance if your first instance is not overloaded. Each instance can handle quite a few requests simultaneously - especially if they do not do anything except for ...