在Python的gevent框架下執(zhí)行異步的Solr查詢的教程
我經(jīng)常需要用Python與solr進(jìn)行異步請(qǐng)求工作。這里有段代碼阻塞在Solr http請(qǐng)求上, 直到第一個(gè)完成才會(huì)執(zhí)行第二個(gè)請(qǐng)求,代碼如下:
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庫(kù)進(jìn)行http請(qǐng)求)
通過(guò)腳本把文檔索引到Solr, 進(jìn)而可以并行工作是很好的。我需要擴(kuò)展我的工作,因此索引瓶頸是Solr,而不是網(wǎng)絡(luò)請(qǐng)求。
不幸的是,當(dāng)進(jìn)行異步編程時(shí)python不像Javascript或Go那樣方便。但是,gevent庫(kù)能給我們帶來(lái)些幫助。gevent底層用的是libevent庫(kù),構(gòu)建于原生異步調(diào)用(select, poll等原始異步調(diào)用),libevent很好的協(xié)調(diào)很多低層的異步功能。
使用gevent很簡(jiǎn)單,讓人糾結(jié)的一點(diǎn)就是thegevent.monkey.patch_all(), 為更好的與gevent的異步協(xié)作,它修補(bǔ)了很多標(biāo)準(zhǔn)庫(kù)。聽(tīng)起來(lái)很恐怖,但是我還沒(méi)有在使用這個(gè)補(bǔ)丁實(shí)現(xiàn)時(shí)遇到 問(wèn)題。
事不宜遲,下面就是你如果用gevents來(lái)并行Solr請(qǐng)求:
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代碼簡(jiǎn)潔,但是它能完成相應(yīng)的工作,代碼的精髓是下面幾行:
# 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產(chǎn)生searcher.search, 我們可以對(duì)產(chǎn)生的任務(wù)進(jìn)行操作,然后我們可以隨意的等著所有產(chǎn)生的任務(wù)完成,最后導(dǎo)出結(jié)果。
差不多就這樣子.如果你有任何想法請(qǐng)給我們留言。讓我們知道我們?nèi)绾文転槟愕腟olr搜索應(yīng)用提供幫助。
- python3中celery異步框架簡(jiǎn)單使用+守護(hù)進(jìn)程方式啟動(dòng)
- python 5個(gè)頂級(jí)異步框架推薦
- python異步Web框架sanic的實(shí)現(xiàn)
- 關(guān)于Python核心框架tornado的異步協(xié)程的2種方法詳解
- 200行自定義python異步非阻塞Web框架
- Python的Tornado框架實(shí)現(xiàn)異步非阻塞訪問(wèn)數(shù)據(jù)庫(kù)的示例
- Python的Tornado框架的異步任務(wù)與AsyncHTTPClient
- Python的Twisted框架上手前所必須了解的異步編程思想
- Python的Tornado框架異步編程入門實(shí)例
- 簡(jiǎn)單介紹Python的Tornado框架中的協(xié)程異步實(shí)現(xiàn)原理
- python 常用的異步框架匯總整理
相關(guān)文章
python 初始化一個(gè)定長(zhǎng)的數(shù)組實(shí)例
今天小編就為大家分享一篇python 初始化一個(gè)定長(zhǎng)的數(shù)組實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python自動(dòng)化測(cè)試?yán)鱯elenium詳解
Selenium是一種常用的Web自動(dòng)化測(cè)試工具,支持多種編程語(yǔ)言和多種瀏覽器,可以模擬用戶的交互行為,自動(dòng)化地執(zhí)行測(cè)試用例和生成測(cè)試報(bào)告。Selenium基于瀏覽器驅(qū)動(dòng)實(shí)現(xiàn),結(jié)合多種定位元素的方法,可以實(shí)現(xiàn)各種復(fù)雜的Web應(yīng)用程序的測(cè)試2023-04-04
python如何實(shí)現(xiàn)不用裝飾器實(shí)現(xiàn)登陸器小程序
這篇文章主要介紹了python如何實(shí)現(xiàn)不用裝飾器實(shí)現(xiàn)登陸器小程序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Python?ChineseCalendar包主要類和方法詳解
ChineseCalendar?是一個(gè)?Python?包,用于獲取中國(guó)傳統(tǒng)日歷信息。這個(gè)包提供了中國(guó)農(nóng)歷、二十四節(jié)氣、傳統(tǒng)節(jié)日、黃歷等信息,這篇文章主要介紹了Python?ChineseCalendar包簡(jiǎn)介,需要的朋友可以參考下2023-03-03
python實(shí)現(xiàn)查找excel里某一列重復(fù)數(shù)據(jù)并且剔除后打印的方法
這篇文章主要介紹了python實(shí)現(xiàn)查找excel里某一列重復(fù)數(shù)據(jù)并且剔除后打印的方法,涉及Python使用xlrd模塊操作Excel的相關(guān)技巧,需要的朋友可以參考下2015-05-05

