国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

在Python的gevent框架下執行異步的Solr查詢的教程

來源:懂視網 責編:小采 時間:2020-11-27 14:32:38
文檔

在Python的gevent框架下執行異步的Solr查詢的教程

在Python的gevent框架下執行異步的Solr查詢的教程:我經常需要用Python與solr進行異步請求工作。這里有段代碼阻塞在Solr http請求上, 直到第一個完成才會執行第二個請求,代碼如下: import requests #Search 1 solrResp = requests.get('http://mysolr.com/solr/stat
推薦度:
導讀在Python的gevent框架下執行異步的Solr查詢的教程:我經常需要用Python與solr進行異步請求工作。這里有段代碼阻塞在Solr http請求上, 直到第一個完成才會執行第二個請求,代碼如下: import requests #Search 1 solrResp = requests.get('http://mysolr.com/solr/stat

我經常需要用Python與solr進行異步請求工作。這里有段代碼阻塞在Solr http請求上, 直到第一個完成才會執行第二個請求,代碼如下:

import requests
 
#Search 1
solrResp = requests.get('http://mysolr.com/solr/statedecoded/search?q=law')
 
for doc in solrResp.json()['response']['docs']:
 print doc['catch_line']
 
#Search 2
solrResp = requests.get('http://mysolr.com/solr/statedecoded/search?q=shoplifting')
 
for doc in solrResp.json()['response']['docs']:
 print doc['catch_line']

(我們用Requests庫進行http請求)

通過腳本把文檔索引到Solr, 進而可以并行工作是很好的。我需要擴展我的工作,因此索引瓶頸是Solr,而不是網絡請求。


不幸的是,當進行異步編程時python不像Javascript或Go那樣方便。但是,gevent庫能給我們帶來些幫助。gevent底層用的是libevent庫,構建于原生異步調用(select, poll等原始異步調用),libevent很好的協調很多低層的異步功能。

使用gevent很簡單,讓人糾結的一點就是thegevent.monkey.patch_all(), 為更好的與gevent的異步協作,它修補了很多標準庫。聽起來很恐怖,但是我還沒有在使用這個補丁實現時遇到 問題。


事不宜遲,下面就是你如果用gevents來并行Solr請求:

import requests
from gevent import monkey
import gevent
monkey.patch_all()
 
 
class Searcher(object):
 """ Simple wrapper for doing a search and collecting the
 results """
 def __init__(self, searchUrl):
 self.searchUrl = searchUrl
 
 def search(self):
 solrResp = requests.get(self.searchUrl)
 self.docs = solrResp.json()['response']['docs']
 
 
def searchMultiple(urls):
 """ Use gevent to execute the passed in urls;
 dump the results"""
 searchers = [Searcher(url) for url in urls]
 
 # Gather a handle for each task
 handles = []
 for searcher in searchers:
 handles.append(gevent.spawn(searcher.search))
 
 # Block until all work is done
 gevent.joinall(handles)
 
 # Dump the results
 for searcher in searchers:
 print "Search Results for %s" % searcher.searchUrl
 for doc in searcher.docs:
 print doc['catch_line']
 
searchUrls = ['http://mysolr.com/solr/statedecoded/search?q=law',
 'http://mysolr.com/solr/statedecoded/search?q=shoplifting']


searchMultiple(searchUrls)
代碼增加了,而且不如相同功能的Javascript代碼簡潔,但是它能完成相應的工作,代碼的精髓是下面幾行:

# Gather a handle for each task
handles = []
for searcher in searchers:
 handles.append(gevent.spawn(searcher.search))
 
# Block until all work is done
gevent.joinall(handles)

我們讓gevent產生searcher.search, 我們可以對產生的任務進行操作,然后我們可以隨意的等著所有產生的任務完成,最后導出結果。

差不多就這樣子.如果你有任何想法請給我們留言。讓我們知道我們如何能為你的Solr搜索應用提供幫助。

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

在Python的gevent框架下執行異步的Solr查詢的教程

在Python的gevent框架下執行異步的Solr查詢的教程:我經常需要用Python與solr進行異步請求工作。這里有段代碼阻塞在Solr http請求上, 直到第一個完成才會執行第二個請求,代碼如下: import requests #Search 1 solrResp = requests.get('http://mysolr.com/solr/stat
推薦度:
標簽: so python 異步
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 亚洲精品乱码久久久久久中文字幕 | 激情另类国内一区二区视频 | 国产精品免费看 | 久久久香蕉 | 国产在线每日更新 | www.com黄色 | 国产成人综合久久精品亚洲 | 91欧美激情一区二区三区成人 | 国产精品福利一区二区久久 | 免费一级a毛片在线播 | 一道精品一区二区三区 | 亚洲欧美另类视频 | 黄色国产在线 | 精品欧美一区二区三区在线观看 | 91频道| 亚洲一区中文字幕在线观看 | 国产高清在线免费视频 | 亚洲国产精品婷婷久久久久 | 日本高清天码一区在线播放 | 久久久精品国产 | 999久久久免费精品国产牛牛 | 国产精品久久久精品三级 | 久久久久久国产精品视频 | 亚洲国产成人久久综合一区 | 欧美 亚洲 校园 第一页 | 国产91精品久久久久久 | 国产91精品一区二区麻豆亚洲 | 国产精品成人69xxx免费视频 | 亚洲欧美日韩高清一区二区三区 | 在线观看免费精品国产 | 中文字幕日韩欧美 | 日韩a无v码在线播放免费 | 亚洲娇小性色xxxx | 亚洲视频在线观看视频 | 日本免费大黄 | 亚洲欧美中文日韩在线 | 91精品欧美一区二区三区 | 欧美 日韩 高清 | 国产精彩视频在线观看 | 欧美激情一区二区亚洲专区 | 黄色在线观看免费 |