在我的代码中,我正在尝试使用多处理来查找给定URL的每个硬币的最高价格。我必须获取大约 1400 个硬币的数据,所以我实现了 Python 的多处理池。我不确定我是否正确使用它,但我遵循了本网站给出的示例:https://docs.python.org/3.4/library/multiprocessing.html?highlight=process
这是我的代码:
import requests
import json
from bs4 import BeautifulSoup
from multiprocessing import Pool
max_prices = []
def find_max (url):
# finds maximum price of a coin
r = requests.get(url)
cont = r.json()
prices = list(map(lambda x: x[1], cont["price_usd"]))
maxPrice = max(prices)
return maxPrice
with open("coins.txt", "r") as f:
data = json.load(f)
coin_slug = [d["slug"] for d in data]
coin_names = [d["name"] for d in data]
urls = []
for item in coin_slug:
url = "https://graphs2.coinmarketcap.com/currencies/"+item+"/"
urls.append(url)
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(find_max, urls)
当我添加这部分代码时,它给了我一个EOF错误:
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(find_max, urls)
最后一行的括号不平衡。它应该是print(p.map(find_max, urls))
.