python-3.x - multiprocessing pool with a dictionary as one of the arguments?
问题描述
Is it possible to use Pool.map()
on a function that contains an empty dictionary as one of its arguments? I am new to multiprocessing
and want to parallise a web-scraping function. I tried following the example from this site however it doesn't include a dictionary as one of the arguments. The multiprocess function works (it prints out the search result), however it does not append to the dictionary, after completing the process the dictionary is still empty. Looks like I have to use Manager()
however I don't know how to implement it. use of Manager() Thanks for help.
from functools import partial
from multiprocessing import Pool
from bs4 import BeautifulSoup as soup
count = 1
outerDict = dict()
emptyList = []
lstOfItems = ['Valsartan','Estrace','Norvasc','Combivent',
'Fluvirin','Kariva','Natrl','Foxamax','Vilanterol','Catapres']
def process_search():
'''a function that scrapes a site; the outerDict and emptyLst will
become populated as it scrapes the site for each item'''
def callSrch(item,outerDict,emptyList,count):
searchlink = 'http://www.asite.com'
uClient=ureq(searchlink+item)
pagehtml = uClient.read()
soupPage_ = soup(pagehtml,'html.parser')
process_search(item,soupPage_,outerDict,count,emptyList)
with Pool() as p:
prfx = partial(callSrch,outerDict=outerDict,emptyList=emptyList,count=count)
p.map(prfx, lstOfItems)
解决方案
推荐阅读
- python - 用 Beatifulsoup 试吃
- sql - 在where子句中的SQL错误未知列?
- c# - 聊天框不工作 c#,Visual Studio,Winforms,Bunifu UI
- javascript - 来自后端的 Vue.js 图像数组作为对象数组返回到前端
- python - 为什么在 Python 中使用 Ray 并行化任务时会出现 PicklingError?
- node.js - AWS DynamoDB 条件获取
- python-3.x - 数值作为字符串并转换为实际数值
- shell - awk:解析文件时丢失数据
- microservices - 对于基于 Spring 的微服务,我们如何从数据湖中推送和拉取数据。.如何使用微服务与数据湖中的数据进行交互
- php - 使用 PHP MySQL 选择和更新多个 id 的单选选项