- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
<分区>
我有一个应用程序(网络应用程序)让用户使用 Twitter oauth 登录并提供自动删除推文的功能。在用户登录到网络应用程序后,我将为每个将删除用户推文列表的用户启动一个 goroutines(通过 REST api)。
假设有 100 个用户,每个用户有 500+ 条推文:
如何在删除过程中停止删除 go 例程。
例如:用户 30 在启动删除过程 2 分钟后请求停止删除推文(这应该通过对我的应用程序的 API 调用来完成)。
信息:我正在使用 anaconda对于 Twitter 客户端后端
编辑:
我找到了一种使用带上下文的 map 来实现它的方法。这是供引用的代码。归功于https://gist.github.com/montanaflynn/020e75c6605dbe2c726e410020a7a974
package main
import (
"context"
"fmt"
"log"
"net/http"
"sync"
"time"
)
// a concurrent safe map type by embedding sync.Mutex
type cancelMap struct {
sync.Mutex
internal map[string]context.CancelFunc
}
func newCancelMap() *cancelMap {
return &cancelMap{
internal: make(map[string]context.CancelFunc),
}
}
func (c *cancelMap) Get(key string) (value context.CancelFunc, ok bool) {
c.Lock()
result, ok := c.internal[key]
c.Unlock()
return result, ok
}
func (c *cancelMap) Set(key string, value context.CancelFunc) {
c.Lock()
c.internal[key] = value
c.Unlock()
}
func (c *cancelMap) Delete(key string) {
c.Lock()
delete(c.internal, key)
c.Unlock()
}
// create global jobs map with cancel function
var jobs = newCancelMap()
// the pretend worker will be wrapped here
// https://siadat.github.io/post/context
func work(ctx context.Context, id string) {
for {
select {
case <-ctx.Done():
fmt.Printf("Cancelling job id %s\n", id)
return
case <-time.After(time.Second):
fmt.Printf("Doing job id %s\n", id)
}
}
}
func startHandler(w http.ResponseWriter, r *http.Request) {
// get job id and name from query parameters
id := r.URL.Query().Get("id")
// check if job already exists in jobs map
if _, ok := jobs.Get(id); ok {
fmt.Fprintf(w, "Already started job id: %s\n", id)
return
}
// create new context with cancel for the job
ctx, cancel := context.WithCancel(context.Background())
// save it in the global map of jobs
jobs.Set(id, cancel)
// actually start running the job
go work(ctx, id)
// return 200 with message
fmt.Fprintf(w, "Job id: %s has been started\n", id)
}
func stopHandler(w http.ResponseWriter, r *http.Request) {
// get job id and name from query parameters
id := r.URL.Query().Get("id")
// check for cancel func from jobs map
cancel, found := jobs.Get(id)
if !found {
fmt.Fprintf(w, "Job id: %s is not running\n", id)
return
}
// cancel the jobs
cancel()
// delete job from jobs map
jobs.Delete(id)
// return 200 with message
fmt.Fprintf(w, "Job id: %s has been canceled\n", id)
}
func main() {
http.HandleFunc("/start", startHandler)
http.HandleFunc("/stop", stopHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
发件人:http://blog.nindalf.com/how-goroutines-work/ As the goroutines are scheduled cooperatively, a go
很多时候在用 Go 开发 http 服务器时,我都会遇到这种困境。 假设我想尽快用http statuscode 200响应客户端(然后在后面执行工作),这就是我通常这样做的原因: 我让我的主要 ht
这是代码: import "fmt" func main() { messages := make(chan string, 1) go func(c chan string) {
我正在学习 Golang,但遇到了一些困难。我已经研究过 Google,但没有任何进展。 我编写了一个代码,通过多台服务器的 ICMP 检查 RTT。 它有这样的结构: type Server str
我想运行多个 goroutine,进行一些处理,将结果放入 channel ,当至少有一个 goroutine 完成时,完成所有其他 goroutine 并从 channel 返回结果。 所以,我尝试
我有两个(但以后我会是三个)go 例程来处理来自远程服务器(来自 ampq channel )的传入消息。但是因为它们正在处理相同的数据/状态,所以我想阻止所有其他 go 例程,除了正在运行的例程。
我有一个案例,我从 2 个不同的位置(ES 和 REDIS)读取数据,我需要从最快的源读取一个值,因此我触发了 2 个 goroutines,一个从 ES 获取数据,其他从REDIS获取。 一旦从其中
像这里一样,我创建了一个 go playground 示例:sGgxEh40ev ,但无法正常工作。 quit := make(chan bool) res := make(chan int) go
我是golang的新手,正在研究goroutine。 我写了一个简单的代码,故意使用 goroutine 来划分数字。 首先,我给出基数并继续除它的数,直到它不能被整除 但是,我改变了go split
Main { go routine_1(carryout a time consuming task and return output) go routine_2(wait for output f
我想知道从另一个 goroutine 返回时调用的 goroutine 会发生什么。他们是继续运行还是被终止?这是一个示例代码来说明我的意思: func func() { // Doing s
更具体地说,在我的例子中,我有一个网络服务器和一个全局可访问的结构,网络服务器使用它来生成页面。我有另一个 Goroutine,它总是定期用新值更新该结构。这会引起问题吗?我是否需要实现一种机制来确保
来自 this file ,我不明白为什么函数startWorker会这样写: func (p *WorkerPool) dispatch() { for i := 0; i < p.maxW
我正在学习围棋,但在使用 goroutines 时遇到了问题。这是我的代码 package main import ( "fmt" "sync" "time" ) var co
我收到以下错误,我不明白为什么: 发送:查询 Herefatal 错误:所有 goroutines 都睡着了 - 死锁! 您可以看到我正在调用我使用 goroutine 创建的函数 routine。我
大家好,我正在从 Python3 过渡到 Go,所以我正在尝试重写我创建的库以获得更好的性能。 我面临一个问题,因为我是 Golang XD 中的新手,我使用有限的 API 下载数百个 json,我想
我有以下格式的脚本部分: func main() { for i=0;i<1000000;i++ { go test() } } func test() { a := test
package main func main() { c:=make(chan int) for i:=0; i<=100;i++ {
我正在学习 Go,我的第一个项目是一个简单的 ping 脚本。本质上,我想 ping 一堆 url,并在每个响应时等待 XXX 秒,然后再次 ping。这是删减的代码: func mai
这个问题在这里已经有了答案: Go all goroutines are asleep deadlock (2 个回答) fatal error: all goroutines are asleep
我是一名优秀的程序员,十分优秀!