我想问是否有大的文本文件可以逐行读取的解决方案
keyword = open('./list.txt', 'r', encoding="utf-8").read().splitlines()
with Pool(processes=2) as executor:
executor.map(verify, keyword)
我只想知道如何逐行读取并将其处理到executor --我找不到它的解决方案,因为它的工作引起了我对多进程的处理--我尝试过使用下面的代码
file1 = open('./list.txt', 'r', encoding="utf-8")
Lines = file1.readlines()
def keyword():
for line in Lines:
return line
但我有个错误
Traceback (most recent call last):
File "main.py", line 40, in <module>
executor.map(verify, keyword)
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/multiprocessing/pool.py", line 364, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/multiprocessing/pool.py", line 475, in _map_async
iterable = list(iterable)
TypeError: 'function' object is not iterable
发布于 2022-10-18 06:01:20
您的错误在于:
def keyword():
因此,当您调用executor.map(verify, keyword)
时,map试图在keyword
上迭代,这是一个函数。
你只需要替换:
def keyword():
通过:
def verify(line):
verify
至少需要一个参数,因为map
将在keyword
上迭代并在每一行上调用verify
。
https://stackoverflow.com/questions/74112201
复制