我正在对涉及URL的项目使用多处理。我注意到,无论我使用什么迭代器,无论何时我使用pool.imap_unordered()
(让我们说它是一个由数字1和2组成的列表,即2个数字),它都会用一个线程运行一次程序,因为列表中有2个数字,它将再次运行。我似乎无法弄清楚。我以为我知道应该做什么。 (不,无论我有多少线程,它都不会运行得更快)(args.urls最初是一个文件,然后我将文件中的所有内容转换为列表),一切正常,直到添加了多处理,所以我知道在我的非多处理相关代码中不会出错。
from multiprocessing import Pool
import multiprocessing
import requests
arrange = [ lines.replace("\n", "") for lines in #file ]
def check():
for lines in arrange:
requests.get(lines)
def main():
pool = ThreadPool(4)
results = pool.imap_unordered(check, arrange)
答案 0 :(得分:1)
所以我不确定您要做什么,但这也许正是您所需要的:
from multiprocessing import ThreadPool
import multiprocessing
import requests
arrange = [ line.replace("\n", "") for line in #file ]
def check(line):
requests.get(line) # remove the loop, since you are using multiprocessing this is not needed as you pass only one of the lines per thread.
def main():
pool = ThreadPool(4)
results = pool.imap_unordered(check, arrange) # This loops through arrange and provides the check function a single line per call