当前位置: 首页 > news >正文

天津网站建设托管查找网站

天津网站建设托管,查找网站,手机兼职赚钱平台,正规的南昌网站建设Hi, there! 这是一份根据 MIT 6.824(2021) 课程的第 2 课的课堂示例代码改编的 2 个 go 语言编程练习。像其他的编程作业一样,我去除了核心部分,保留了代码框架,并编写了每一步的提示 练习代码在本文的最后面 爬虫 在第一部分,…

Hi, there! 这是一份根据 MIT 6.824(2021) 课程的第 2 课的课堂示例代码改编的 2 个 go 语言编程练习。像其他的编程作业一样,我去除了核心部分,保留了代码框架,并编写了每一步的提示

练习代码在本文的最后面

爬虫

在第一部分,你需要实现 3 个版本的网络爬虫。

1 单线程爬虫

首先,请为 fakeFetcher 类型实现 Fetcher 接口中的 Fetch() 方法。然后实现串行爬虫 Serial() 函数(递归),并在 main() 中调用它,预期的输出如下:

=== Serial===
found: http://golang.org/
found: http://golang.org/pkg/
missing: http://golang.org/cmd/
found: http://golang.org/pkg/fmt/
found: http://golang.org/pkg/os/

2 多线程爬虫(使用锁同步)

我们定义了 fetchState 类型,用于对 fetched 加锁保护,但是还未实现它的“构造函数”。请先实现它的“构造函数”,名为 makeState()。注意对于结构体,一般返回其指针

然后实现 ConcurrentMutex,实现一个通过锁控制的并发爬虫。提示:

sync.WaitGroup(等待组)是Go语言标准库中的一个并发原语,用于等待一组 goroutine 完成执行,提供了三个主要方法:

  • Add(delta int):增加等待组的计数器。delta 参数表示要添加到计数器的值,通常为 1
  • Done():减少等待组的计数器。相当于 Add(-1)
  • Wait():阻塞调用它的 goroutine,直到等待组的计数器归零

等待组的计数器是一个非负整数,初始值为 0。当计数器的值变为 0 时,意味着所有的 goroutine 都已经完成,Wait() 方法会解除阻塞并继续执行后续代码

最后,在 main 中调用 ConcurrentMutex,预期的输出如下:

=== Serial===
found: http://golang.org/
found: http://golang.org/pkg/
missing: http://golang.org/cmd/
found: http://golang.org/pkg/fmt/
found: http://golang.org/pkg/os/
=== ConcurrentMutex ===
found: http://golang.org/
missing: http://golang.org/cmd/
found: http://golang.org/pkg/
found: http://golang.org/pkg/os/
found: http://golang.org/pkg/fmt/

3 多线程爬虫(使用channel同步)

练习代码中已经提供了 ConcurrentChannel 函数,你无需改动它,只需要实现 workermaster 函数即可

// Concurrent crawler with channels
func ConcurrentChannel(url string, fetcher Fetcher) {ch := make(chan []string)go func() {ch <- []string{url}}()master(ch, fetcher)
}

最后,在 main 中调用 ConcurrentChannel 函数,预期的输出如下:

=== Serial===
found: http://golang.org/
found: http://golang.org/pkg/
missing: http://golang.org/cmd/
found: http://golang.org/pkg/fmt/
found: http://golang.org/pkg/os/
=== ConcurrentMutex ===
found: http://golang.org/
missing: http://golang.org/cmd/
found: http://golang.org/pkg/
found: http://golang.org/pkg/os/
found: http://golang.org/pkg/fmt/
=== ConcurrentChannel ===
found: http://golang.org/
missing: http://golang.org/cmd/
found: http://golang.org/pkg/
found: http://golang.org/pkg/os/
found: http://golang.org/pkg/fmt/

kv 存储

在第二部分,你需要实现一个基于 RPC 的 KV 存储服务

首先你需要为 *KV 实现 GetPut 方法,它们都返回 error 类型。然后补全 get 函数和 put 函数,使得它们能够在 main 中正常工作

提示:你可以通过下面 3 行代码调用 server 中已经注册的 KV.Get 服务:

client := connect()
err := client.Call("KV.Get", &args, &reply)
client.Close()

完成后,预期的输出如下:

Put(subject, 6.824) done
get(subject) -> 6.824

练习代码

// crawler-exercise.go
package mainimport ("sync"
)// Serial crawler
func Serial(url string, fetcher Fetcher, fetched map[string]bool) {// TODO 1
}// Concurrent crawler with shared state and Mutex
type fetchState struct {mu      sync.Mutexfetched map[string]bool
}// TODO 2: implement fetchState's constructorfunc ConcurrentMutex(url string, fetcher Fetcher, f *fetchState) {// TODO 2
}func worker(url string, ch chan []string, fetcher Fetcher) {// TODO 3
}func master(ch chan []string, fetcher Fetcher) {// TODO 3
}// Concurrent crawler with channels
func ConcurrentChannel(url string, fetcher Fetcher) {ch := make(chan []string)go func() {ch <- []string{url}}()master(ch, fetcher)
}func main() {// uncomment them step by step/*fmt.Printf("=== Serial===\n")Serial("http://golang.org/", fetcher, make(map[string]bool))fmt.Printf("=== ConcurrentMutex ===\n")ConcurrentMutex("http://golang.org/", fetcher, makeState())fmt.Printf("=== ConcurrentChannel ===\n")ConcurrentChannel("http://golang.org/", fetcher)*/
}// Fetcher
type Fetcher interface {// Fetch returns a slice of URLs found on the page.Fetch(url string) (urls []string, err error)
}// fakeFetcher is Fetcher that returns canned results.
type fakeFetcher map[string]*fakeResulttype fakeResult struct {body stringurls []string
}// TODO 1: implement Fetch for fakeFetch// fetcher is a populated fakeFetcher.
var fetcher = fakeFetcher{"http://golang.org/": &fakeResult{"The Go Programming Language",[]string{"http://golang.org/pkg/","http://golang.org/cmd/",},},"http://golang.org/pkg/": &fakeResult{"Packages",[]string{"http://golang.org/","http://golang.org/cmd/","http://golang.org/pkg/fmt/","http://golang.org/pkg/os/",},},"http://golang.org/pkg/fmt/": &fakeResult{"Package fmt",[]string{"http://golang.org/","http://golang.org/pkg/",},},"http://golang.org/pkg/os/": &fakeResult{"Package os",[]string{"http://golang.org/","http://golang.org/pkg/",},},
}
// kv-exercise.go
package mainimport ("fmt""log""net""net/rpc""sync"
)//
// Common RPC request/reply definitions
//const (OK       = "OK"ErrNoKey = "ErrNoKey"
)type Err stringtype PutArgs struct {Key   stringValue string
}type PutReply struct {Err Err
}type GetArgs struct {Key string
}type GetReply struct {Err   ErrValue string
}func connect() *rpc.Client {client, err := rpc.Dial("tcp", ":1234")if err != nil {log.Fatal("dialing:", err)}return client
}func get(key string) string {// TODO 2return ""
}func put(key string, val string) {// TODO 2
}type KV struct {mu   sync.Mutexdata map[string]string
}// TODO 1: implement `Get` method for *KV// TODO 1: implement `Put` method for *KV
func server() {kv := new(KV)kv.data = map[string]string{}rpcs := rpc.NewServer()rpcs.Register(kv)l, e := net.Listen("tcp", ":1234")if e != nil {log.Fatal("listen error:", e)}go func() {for {conn, err := l.Accept()if err == nil {go rpcs.ServeConn(conn)} else {break}}l.Close()}()
}func main() {server()put("subject", "6.824")fmt.Printf("Put(subject, 6.824) done\n")fmt.Printf("get(subject) -> %s\n", get("subject"))
}
http://www.hrbkazy.com/news/53416.html

相关文章:

  • 宾爵手表官方网站太原互联网推广公司
  • 驻马店做网站公司如何做好口碑营销
  • 网站设计素材优化大师电视版
  • wordpress_主题教程济南seo公司
  • 算命公司网站建设制作开发方案seo标题优化的心得总结
  • 江苏省镇江市丹阳市疫情最新消息郑州seo优化外包热狗网
  • 7月新闻大事件30条全专业优化公司
  • 上海网站建设 迈seo文章是什么意思
  • 长沙网站seo收费标准3分钟搞定网站seo优化外链建设
  • 关于内网站建设的请示培训机构哪家好
  • 顺义网站建设廊坊seo排名公司
  • 怎么做网站的后台管理系统手机百度高级搜索
  • 望野什么意思郑州网络seo公司
  • 建设优化一个网站步骤最佳搜索引擎
  • wordpress中文转拼音长春网站优化咨询
  • 企业网站优化的三层含义属于seo网站优化
  • wordpress插件随机文章西安seo报价
  • 中国建设网官方网站发改委移动排名提升软件
  • 网络营销型网站设计竞价托管一般多少钱
  • 网站重要性全部列表支持安卓浏览器软件下载
  • 网上哪里有辅导高考生做难题的网站短链接生成网址
  • 新开传奇网站排行一键识图找原图
  • 怎么做自动跳转网站怎么优化一个网站
  • 做系统后怎么找回网站收藏夹广告联盟下载app
  • 常州网站建设公司如何网络运营需要学什么
  • 网站开发行业竞争大吗网络营销方案设计
  • wordpress 悬浮联系百度seo收录软件
  • wordpress 关闭插件更新优化网站标题和描述的方法
  • 龙华营销型网站建设公司软件开发需要学什么
  • 江苏做网站公司拉人头最暴利的app