小编Jos*_*sep的帖子

如何在 Golang 中实现适当的并行性?goroutines 在 1.5+ 版本中是并行的吗?

我知道并行性和并发性之间的区别。我正在寻找如何在 Go 中实现并行性。我希望 goroutines 是并行的,但我发现的文档似乎相反。

设置 GOMAXPROCS 允许我们配置应用程序可用于并行运行的线程数。从 1.5 版开始,GOMAXPROCS 将核心数作为值。据我了解,从 1.5 版开始,goroutine 本质上是并行的。这个结论正确吗?

我在 StackOverflow 等网站上发现的每个问题似乎都已经过时,并且没有考虑到 1.5 版中的这一变化。请参阅:golang 中的并行处理

我的困惑源于试图在实践中测试这种并行性。我在 Go 1.10 中尝试了以下代码,但它没有并行运行。

package main

import (
    "fmt"
    "sync"
)

var wg sync.WaitGroup

func main() {

    wg.Add(2)
    go count()
    go count()
    wg.Wait()

}

func count() {
    defer wg.Done()
    for i := 0; i < 10; i++ {
        fmt.Println(i)
    }
}
Run Code Online (Sandbox Code Playgroud)

将 GOMAXPROCS 设置为 2 不会改变结果。我得到一个并发程序而不是并行程序。

我在 8 核系统上运行所有测试。

编辑:供将来参考,

我被这个博客迷住了:https : //www.ardanlabs.com/blog/2014/01/concurrency-goroutines-and-gomaxprocs.html,其中在一个小的 for 循环中实现了并行性而没有太多麻烦。@peterSO 的回答是完全有效的。出于某种原因,在 …

parallel-processing concurrency go

7
推荐指数
1
解决办法
4951
查看次数

Python asyncio / aiohttp:ValueError:Windows上的select()中的文件描述符过多

大家好,我在尝试理解asyncio和aiohttp并使两者正常工作方面遇到困难。不仅我不正确地了解自己在做什么,这时我遇到了一个我不知道如何解决的问题。

我正在使用Windows 10 64位最新更新。

以下代码使用asyncio返回了标题中Content-Type中不包含html的页面列表。

import asyncio
import aiohttp

MAXitems = 30

async def getHeaders(url, session, sema):
    async with session:
        async with sema:
            try:
                async with session.head(url) as response:
                    try:
                        if "html" in response.headers["Content-Type"]:
                            return url, True
                        else:
                            return url, False
                    except:
                        return url, False
            except:
                return url, False


def checkUrlsWithoutHtml(listOfUrls):
    headersWithoutHtml = set()
    while(len(listOfUrls) != 0):
        blockurls = []
        print(len(listOfUrls))
        items = 0
        for num in range(0, len(listOfUrls)):
            if num < MAXitems:
                blockurls.append(listOfUrls[num - items])
                listOfUrls.remove(listOfUrls[num - items])
                items …
Run Code Online (Sandbox Code Playgroud)

python python-3.x async-await python-asyncio aiohttp

4
推荐指数
2
解决办法
3526
查看次数