我刚接触多进程编程,正在尝试编写一个程序,在谷歌上查找给定搜索词的前10个结果。在这个例子中,我只想同时运行两个搜索任务。以下是我的代码:
import threading
from multiprocessing.pool import Pool
import pycountry
import bs4
import requests
from googlesearch import search
def getGoogleResults(query):
links = []
# from geeks4geeks
print("Getting google results...")
for j in search(query, tld="co.in", num=10, stop=10, pause=2):
links.append(j)
print("Got google results!")
return links
global queryResults
queryResults = {}
queries = ["stackoverflow", "github"]
if __name__ == "__main__":
with Pool(2) as p:
p.map(getGoogleResults, queries)
然而,当我运行它时,出现以下错误:
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/pool.py", line 771, in get
raise self._value
multiprocessing.pool.MaybeEncodingError: Error sending result: '<multiprocessing.pool.ExceptionWithTraceback object at 0x101b23820>'. Reason: 'TypeError("cannot pickle '_io.BufferedReader' object")'
我找不到任何解决这个问题的地方。非常感谢您的帮助!
我已经大致确定问题出在 .append 部分,但是我不知道该如何修复它。有很多关于此问题的文章,但没有答案。
j
是什么,都不能被 pickled 并发送回主进程。那么j
是什么? - Aaron