每天推薦一個 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