使用Pool时出现EOF错误

时间:2018-06-01 19:45:30

标签: python-3.x multiprocessing python-3.6 python-multiprocessing pool

在我的代码中,我尝试使用多处理来查找给定URL的每个硬币的最高价格。我需要大约1400个硬币来获取数据,所以我实现了Python的多处理池。我不确定我是否正确使用它,但我按照本网站提供的示例:https://docs.python.org/3.4/library/multiprocessing.html?highlight=process

这是我的代码:

public function post(Request $request, $filename)
{
    if (session_id() == "") {
        session_start();
    }

    if (Auth::check()) {
        $_SESSION['userId'] = Auth::user()->userId;
    }

    $filePath = realpath(app_path() . '/../response/' . $filename);
    include $filePath; 
}

当我添加这部分代码时,它给了我一个EOF错误:

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)

1 个答案:

答案 0 :(得分:1)

最后一行中有不平衡的括号。它应该是 print(p.map(find_max, urls))