python - 在多处理 pool.starmap 中传递字符串列表时出错

标签 python python-3.x

我正在尝试一些网页抓取。我正在将包含 URL 的列表传递给 pool.starmap ,但我遇到了参数错误。显示我的代码的简化版本:

有人可以帮我解决这个问题吗? 对不起,如果我做了一些愚蠢的事情。


from multiprocessing.dummy import Pool

def func(x):
    print(x)

s = ["cat","foo","bar","you","and","me"] #this list contains ~50 URLs in actual code

with Pool() as pool:
    pool.starmap(func,s)

这给我错误:

Traceback (most recent call last):                                                                                                          
File "g.py", line 8, in <module>                                                                                                            
  pool.starmap(func,s)                                                                                                                    
File "C:\Users\Gunjan\Anaconda3\lib\multiprocessing\pool.py", line 274, in starmap                                                          return 
  self._map_async(func, iterable, starmapstar, chunksize).get()                                                                    
File "C:\Users\Gunjan\Anaconda3\lib\multiprocessing\pool.py", line 644, in get                                                              
  raise self._value                                                                                                                       
File "C:\Users\Gunjan\Anaconda3\lib\multiprocessing\pool.py", line 119, in worker
  result = (True, func(*args, **kwds))                                                                                                    
File "C:\Users\Gunjan\Anaconda3\lib\multiprocessing\pool.py", line 47, in starmapstar
  return list(itertools.starmap(args[0], args[1]))                                                                                      
TypeError: func() takes 1 positional argument but 3 were given

最佳答案

starmap 需要可迭代列表。检查here .所以请改用 map

关于python - 在多处理 pool.starmap 中传递字符串列表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56686730/

相关文章:

Python 如何在不清除用户输入的情况下清除屏幕?

python-3.x - 如何将特定类型的numpy数组声明为数据类中的类型

python - 在 python 中列出转换的字符串?

python - 值错误: dictionary update sequence element #0 has length 3; 2 is required: NLTK

python的请求显示奇怪的语言而不是阿拉伯语

python - 用于检查哪个串行端口已连接的 Bash 脚本

python - 主脑号码位置值

python - 子进程.Popen : cloning stdout and stderr both to terminal and variables

Python将一维数组转化为二维数组

python - 用于识别关键字的正则表达式