Python+ artificial intelligence employment class v5.0wumi

Time:2022-5-28

download:Python+ artificial intelligence employment class v5.0

package main
import (

"fmt"
"math/rand"
"time"

)
var (

Web   = fakeSearch("web")
Image = fakeSearch("image")
Video = fakeSearch("video")

)
type Result string
type Search func(query string) Result
func fakeSearch(kind string) Search {

return func(query string) Result {
    time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
    return Result(fmt.Sprintf("%s result for %q\n", kind, query))
}

}
func Google(query string) (results []Result) {

results = append(results, Web(query))
results = append(results, Image(query))
results = append(results, Video(query))
return

}
func main() {

rand.Seed(time.Now().UnixNano())
start := time.Now()
results := Google("golang")
elapsed := time.Since(start)
fmt.Println(results)
fmt.Println(elapsed)

}
The operation results are as follows:

[web result for “golang”
image result for “golang”
video result for “golang”
]
153.365484ms

Google search 2.0

Run web, image and video search at the same time and wait for all results. No lock, no condition variable, no callback.

The code is as follows. Pay attention to the Google function.

package main
import (

"fmt"
"math/rand"
"time"

)
var (

Web   = fakeSearch("web")
Image = fakeSearch("image")
Video = fakeSearch("video")

)
type Result string
type Search func(query string) Result
func fakeSearch(kind string) Search {

return func(query string) Result {
    time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
    return Result(fmt.Sprintf("%s result for %q\n", kind, query))
}

}
func Google(query string) (results []Result) {

c := make(chan Result)
go func() { c <- Web(query) } ()
go func() { c <- Image(query) } ()
go func() { c <- Video(query) } ()
for i := 0; i < 3; i++ {
    result := <-c
    results = append(results, result)
}
return

}
func main() {

rand.Seed(time.Now().UnixNano())
start := time.Now()
results := Google("golang")
elapsed := time.Since(start)
fmt.Println(results)
fmt.Println(elapsed)

}

Google search 2.1 don’t wait for a slow server. There are no locks, unconditional variables, and no callbacks. After the timeout of select, the time The timeout channel defined by after is placed outside the for loop.

package main
import (

"fmt"
"math/rand"
"time"

)
var (

Web   = fakeSearch("web")
Image = fakeSearch("image")
Video = fakeSearch("video")

)
type Result string
type Search func(query string) Result
func fakeSearch(kind string) Search {

return func(query string) Result {
    time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
    return Result(fmt.Sprintf("%s result for %q\n", kind, query))
}

}
func Google(query string) (results []Result) {

c := make(chan Result)
go func() { c <- Web(query) } ()
go func() { c <- Image(query) } ()
go func() { c <- Video(query) } ()
timeout := time.After(80 * time.Millisecond)
for i := 0; i < 3; i++ {
    select {
    case result := <-c:
        results = append(results, result)
    case <-timeout:
        fmt.Println("timed out")
        return
    }
}
return

}
func main() {

rand.Seed(time.Now().UnixNano())
start := time.Now()
results := Google("golang")
elapsed := time.Since(start)
fmt.Println(results)
fmt.Println(elapsed)

}