简体   繁体   English

第一个请求未在 goroutine 函数开始时发送

[英]First requests not sending in the start of the goroutine func

i am running goroutines in my code.我在我的代码中运行 goroutines。 say, if i set my threads to 50, it will not run the first 49 requests, but it will run the 50th request and continue with the rest.比如说,如果我将线程设置为 50,它将不会运行前 49 个请求,但它将运行第 50 个请求并继续使用 rest。 i am not really sure how to describe the issue i am having, and it gives no errors.我不确定如何描述我遇到的问题,并且没有错误。 this has only happened while using fasthttp, and works fine with net/http.这仅在使用 fasthttp 时发生,并且适用于 net/http。 could it be an issue with fasthttp?可能是fasthttp的问题吗? (this is not my whole code, just the area where i think the issues are occurring) (这不是我的全部代码,只是我认为问题发生的区域)

    threads := 50
    var Lock sync.Mutex
    semaphore := make(chan bool, threads)

    for len(userArray) != 0 {
        semaphore <- true
        go func() {
            Lock.Lock()
            var values []byte
            defer func() { <-semaphore }()
            fmt.Println(len(userArray))
            if len(userArray) == 0 {
                return
            }
            values, _ = json.Marshal(userArray[0])
            currentArray := userArray[0]
            userArray = userArray[1:]
            client := &fasthttp.Client{
                Dial: fasthttpproxy.FasthttpHTTPDialerTimeout(proxy, time.Second * 5),
            }
            time.Sleep(1 * time.Nanosecond)
            Lock.Unlock()

this is the output i get (the numbers are the amount of requests left)这是我得到的 output(数字是剩余的请求数量)

200
199
198
197
196
195
194
193
192
191
190
189
188
187
186
185
184
183
182
181
180
179
178
177
176
175
174
173
172
171
170
169
168
167
166
165
164
163
162
161
160
159
158
157
156
155
154
153
152
151
(10 lines of output from req 151)
150
(10 lines of output from req 150)
cont.

sorry if my explanation is confusing, i honestly don't know how to explain this error抱歉,如果我的解释令人困惑,我真的不知道如何解释这个错误

I think the problem is with the scoping of the variables.我认为问题在于变量的范围。 In order to represent the queueing, I'd have a pool of parallel worker threads that all pull from the same channel and then wait for them using a waitgroup.为了表示排队,我有一个并行工作线程池,它们都从同一个通道中提取,然后使用等待组等待它们。 The exact code might need to be adapted as I don't have a go compiler at hand, but the idea is like this:可能需要修改确切的代码,因为我手头没有 go 编译器,但想法是这样的:

    threads := 50
    queueSize := 100 // trying to add more into the queue will blocke

    semaphore := make(chan bool, threads)
    jobQueue := make(chan MyItemType, queueSize)

    var wg sync.WaitGroup

    func processQueue(jobQueue <- chan MyItemType) {
      defer wg.Done()
      for item := range jobQueue {
         values, _ = json.Marshal(item) // doesn't seem to be used?
         client := &fasthttp.Client{
            Dial: fasthttpproxy.FasthttpHTTPDialerTimeout(proxy, time.Second * 5),
         }
      }
    }

    for i := 0; i < threads; i++ {
      wg.Add(1)
      go processQueue(jobQueue)
    }
  
    close(jobQueue)
    wg.Wait()

Now you can put items into jobQueue and they will be processed by one of these threads.现在您可以将项目放入jobQueue ,它们将由这些线程之一处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM