每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,欢迎关注开源日报。交流QQ群:202790710;电报群 https://t.me/OpeningSourceOrg
今日推荐开源项目:《异步与异步编程》
推荐理由:今天不算特别介绍某一个开源项目,在第12期开源周报《朝看花开满树红,正在教室学习中》中介绍了 GINO —— 一个为 SQL asyncio 构建的轻量级异步 ORM,而这一次要介绍的就是何为异步。
为完成某个任务,不同程序单元之间过程中无需协调通讯,也能完成任务的方式,就可以称为异步。对于异步,首先,不相关的程序单元之间是可以异步的。例如,爬虫下载网页。调度程序调用下载程序后,即可调度其他任务,而无需与该下载任务保持通信以协调行为。不同网页的下载、保存等操作都是无关的,也无需相互通知协调。这些异步操作的完成时刻并不确定。
简而言之,异步意味着无序。
而异步编程是指以进程、线程、协程、函数/方法作为执行任务程序的基本单位,结合回调、事件循环、信号量等机制,以提高程序整体执行效率和并发能力的编程方式。
如果在某程序的运行时,能根据已经执行的指令准确判断它接下来要进行哪个具体操作,那它是同步程序,反之则为异步程序。这也就是有序与无序的区别。
同步与异步
同步和异步相对。同步指的是不同的程序单元为了完成某个任务而依靠一些通信方式协调一致的方式,而异步指的是不同程序单元无需依靠通信协调也能完成任务的方式。
同步的优缺点
同步流程最大的好处就是有序,可以更简单的处理结果,也能很容易的发现错误并改正。同步流程也符合我们的自然思想,使编程变得简单,程序也更容易掌控。
同步的缺点就在于交互方面,如果通信效率下降或者是执行任务所需要的时间过长,程序就需要花费更多的时间暂停自己去等待任务的完成,而在程序等待着任务完成的这段时间里,它是无法执行其他行动的,这就相当于无谓的浪费时间和资源。
异步的优缺点
异步意味着无序,一个任务开始之后就可以进行其他操作,而它什么时候完成是未定的,而这带来的最大好处就是在等待结果的这段时间里可以进行其他操作,提高了程序的效率。
而异步也是有缺点的,那就是难以控制程序,这个程序什么时候发生了什么事件是不可预料的。而同步代码改为异步代码也会需要重新布置代码结构,精心安排异步的任务,还需要利用回调函数,对于新手来说着实有些困难。
异步编程的前途
异步编程的发展前景,可以说是一片大好,具体原因嘛,看看这些大公司的动作就知道了。
.NET 与C# 的每个版本发布都是有一个“主题”。即:C#1.0托管代码→C#2.0泛型→C#3.0LINQ→C#4.0动态语言→C#5.0异步编程。C#5.0的主题是异步编程,这说明了啥?说明了异步编程是大势所趋。
在2010年的 PDC 上,微软发布了 Visual Studio Async CTP,大大地降低了异步编程的难度,让我们可以像写同步的方法那样去编写异步代码。微软为什么花精力让异步编程变得简单?因为市场需求嘛,这样能到让开发者更轻松。而很多开发者都看好的技术,我觉得是靠谱的。
从上两届 PyCon 技术大会看来,异步编程已经成了 Python 生态下一阶段的主旋律。如新兴的 Go、Rust、Elixir 等编程语言都将其支持异步和高并发作为主要“卖点”,技术变化趋势如此。Python 生态为不落人后,从2013年起由 Python 之父 Guido 亲自操刀主持了 Tulip(asyncio) 项目的开发。很显然,大家都意识到了异步编程将会掀起一场技术的风暴,都纷纷加入异步编程的大军中来。你还能说异步编程前景不好吗?
今日推荐英文原文:《How Google autocomplete works in Search》作者:Danny Sullivan
原文链接:https://www.blog.google/products/search/how-google-autocomplete-works-search/
推荐理由:Google 的自动搜索是怎么完成的呢?很多朋友都很好奇,这里是 Google 官方的答案
How Google autocomplete works in Search
Autocomplete is a feature within Google Search designed to make it faster to complete searches that you’re beginning to type. In this post—the second in a series that goes behind-the-scenes about Google Search—we’ll explore when, where and how autocomplete works.
Using autocomplete
Autocomplete is available most anywhere you find a Google search box, including the Google home page, the Google app for iOS and Android, the quick search box from within Android and the “Omnibox” address bar within Chrome. Just begin typing, and you’ll see predictions appear:
In the example above, you can see that typing the letters “san f” brings up predictions such as “san francisco weather” or “san fernando mission,” making it easy to finish entering your search on these topics without typing all the letters.
Sometimes, we’ll also help you complete individual words and phrases, as you type:
Autocomplete is especially useful for those using mobile devices, making it easy to complete a search on a small screen where typing can be hard. For both mobile and desktop users, it’s a huge time saver all around. How much? Well:
- On average, it reduces typing by about 25 percent
- Cumulatively, we estimate it saves over 200 years of typing time per day. Yes, per day!
Predictions, not suggestions
You’ll notice we call these autocomplete “predictions” rather than “suggestions,” and there’s a good reason for that. Autocomplete is designed to help people complete a search they were intending to do, not to suggest new types of searches to be performed. These are our best predictions of the query you were likely to continue entering.How do we determine these predictions? We look at the real searches that happen on Google and show common and trending ones relevant to the characters that are entered and also related to your location and previous searches.
The predictions change in response to new characters being entered into the search box. For example, going from “san f” to “san fe” causes the San Francisco-related predictions shown above to disappear, with those relating to San Fernando then appearing at the top of the list:
That makes sense. It becomes clear from the additional letter that someone isn’t doing a search that would relate to San Francisco, so the predictions change to something more relevant.
Why some predictions are removed
The predictions we show are common and trending ones related to what someone begins to type. However, Google removes predictions that are against our autocomplete policies, which bar:
- Sexually explicit predictions that are not related to medical, scientific, or sex education topics
- Hateful predictions against groups and individuals on the basis of race, religion or several other demographics
- Violent predictions
- Dangerous and harmful activity in predictions
In addition to these policies, we may remove predictions that we determine to be spam, that are closely associated with piracy, or in response to valid legal requests.
A guiding principle here is that autocomplete should not shock users with unexpected or unwanted predictions.
This principle and our autocomplete policies are also why popular searches as measured in our Google Trends tool might not appear as predictions within autocomplete. Google Trends is designed as a way for anyone to deliberately research the popularity of search topics over time. Autocomplete removal policies are not used for Google Trends.
Why inappropriate predictions happen
We have systems in place designed to automatically catch inappropriate predictions and not show them. However, we process billions of searches per day, which in turn means we show many billions of predictions each day. Our systems aren’t perfect, and inappropriate predictions can get through. When we’re alerted to these, we strive to quickly remove them.
It’s worth noting that while some predictions may seem odd, shocking or cause a “Who would search for that!” reaction, looking at the actual search results they generate sometimes provides needed context. As we explained earlier this year, the search results themselves may make it clearer in some cases that predictions don’t necessarily reflect awful opinions that some may hold but instead may come from those seeking specific content that’s not problematic. It’s also important to note that predictions aren’t search results and don’t limit what you can search for.
Regardless, even if the context behind a prediction is good, even if a prediction is infrequent, it’s still an issue if the prediction is inappropriate. It’s our job to reduce these as much as possible.
Our latest efforts against inappropriate predictions
To better deal with inappropriate predictions, we launched a feedback tool last year and have been using the data since to make improvements to our systems. In the coming weeks, expanded criteria applying to hate and violence will be in force for policy removals.
Our existing policy protecting groups and individuals against hateful predictions only covers cases involving race, ethnic origin, religion, disability, gender, age, nationality, veteran status, sexual orientation or gender identity. Our expanded policy for search will cover any case where predictions are reasonably perceived as hateful or prejudiced toward individuals and groups, without particular demographics.
With the greater protections for individuals and groups, there may be exceptions where compelling public interest allows for a prediction to be retained. With groups, predictions might also be retained if there’s clear “attribution of source” indicated. For example, predictions for song lyrics or book titles that might be sensitive may appear, but only when combined with words like “lyrics” or “book” or other cues that indicate a specific work is being sought.
As for violence, our policy will expand to cover removal of predictions which seem to advocate, glorify or trivialize violence and atrocities, or which disparage victims.
How to report inappropriate predictions
Our expanded policies will roll out in the coming weeks. We hope that the new policies, along with other efforts with our systems, will improve autocomplete overall. But with billions of predictions happening each day, we know that we won’t catch everything that’s inappropriate.
Should you spot something, you can report using the “Report inappropriate predictions” link we launched last year, which appears below the search box on desktop:
For those on mobile or using the Google app for Android, long press on a prediction to get a reporting option. Those using the Google app on iOS can swipe to the left to get the reporting option.
By the way, if we take action on a reported prediction that violates our policies, we don’t just remove that particular prediction. We expand to ensure we’re also dealing with closely related predictions. Doing this work means sometimes an inappropriate prediction might not immediately disappear, but spending a little extra time means we can provide a broader solution.
Making predictions richer and more useful
As said above, our predictions show in search boxes that range from desktop to mobile to within our Google app. The appearance, order and some of the predictions themselves can vary along with this.
When you’re using Google on desktop, you’ll typically see up to 10 predictions. On a mobile device, you’ll typically see up to five, as there’s less screen space.
On mobile or Chrome on desktop, we may show you information like dates, the local weather, sports information and more below a prediction:
In the Google app, you may also notice that some of the predictions have little logos or images next to them. That’s a sign that we have special Knowledge Graph information about that topic, structured information that’s often especially useful to mobile searchers:
Predictions also will vary because the list may include any related past searches you’ve done. We show these to help you quickly get back to a previous search you may have conducted:
You can tell if a past search is appearing because on desktop, you’ll see the word “Remove” appear next to a prediction. Click on that word if you want to delete the past search.
On mobile, you’ll see a clock icon on the left and an X button on the right. Click on the X to delete a past search. In the Google App, you’ll also see a clock icon. To remove a prediction, long press on it in Android or swipe left on iOS to reveal a delete option.
You can also delete all your past searches in bulk, or by particular dates or those matching particular terms using My Activity in your Google Account.
More about autocomplete
We hope this post has helped you understand more about autocomplete, including how we’re working to reduce inappropriate predictions and to increase the usefulness of the feature. For more, you can also see our help page about autocomplete.
You can also check out the recent Wired video interview below, where our our vice president of search Ben Gomes and the product manager of autocomplete Chris Haire answer questions about autocomplete that came from…autocomplete!
每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,欢迎关注开源日报。交流QQ群:202790710;电报群 https://t.me/OpeningSourceOrg