gpt4 book ai didi

python - 使用 python并发.futures 提交进程编号

转载 作者:行者123 更新时间:2023-12-01 09:19:41 25 4
gpt4 key购买 nike

我正在尝试使用 python 3 迭代对网站的多进程查询。我使用了 https://docs.python.org/3/library/concurrent.futures.html 中的示例代码的修改版本。示例 17.4.2.1。 ThreadPoolExecutor 示例

    with concurrent.futures.ProcessPoolExecutor(max_workers=self.load) as executor:
futureThreadVer = {executor.submit(self.get_code(), 60):x for x in range(self.load)}
for future in concurrent.futures.as_completed(futureThreadVer):
threadVer = futureThreadVer[future]
try:
data=future.result()
except Exception as exc:
print("%r generated an exception : %s" %(threadVer, exc))
else:
print("%r page data %r" % (self.page, data))

它是对https://docs.python.org/3/library/concurrent.futures.html中的示例代码的修改示例 17.4.2.1。 ThreadPoolExecutor 示例

我不想将线程映射到 url,而是希望附加一个进程号,因为我在不同进程中查询相同的 url。

我收到以下错误:

3 generated an exception : 'int' object is not callable
1 generated an exception : 'int' object is not callable
0 generated an exception : 'int' object is not callable
2 generated an exception : 'int' object is not callable
4 generated an exception : 'int' object is not callable

最佳答案

在第二行中:

futureThreadVer = {executor.submit(self.get_code(), 60):x for x in range(self.load)}

executor.submit的调用包含一个函数调用。相反,它应该是对该函数的引用。因此,发生的情况不是在执行程序内部调用该函数,而是在主线程中调用该函数,并且 self.get_code 可能返回一个实际传递给执行程序的整数。然后执行器尝试调用该整数,认为它是一个函数。发生这种情况是因为 python 支持鸭子类型,并且执行器期望第一个参数是函数对象。

因此将第二行更改为:

futureThreadVer = {executor.submit(self.get_code, 60):x for x in range(self.load)}

关于python - 使用 python并发.futures 提交进程编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50900825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com