日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Async Solr Queries in Python

發布時間:2023/12/20 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Async Solr Queries in Python 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

I frequently hit the wall of needing to work asynchronously with Solr requests in Python. I’ll have some code that blocks on a Solr HTTP request, waits for it to complete, then execute a second request. Something like this code

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'] (we’re using the Requests library to do HTTP):

Being able to parallelize work is especially helpful with scripts that index documents into Solr. I need to scale my work up so that Solr, not network access, is the indexing bottleneck.

Unfortunately, Python isn’t exactly Javascript or Go when it comes to doing asynchronous programming. But the gevent coroutine library can help us a bit with that. Under the hood gevent uses the libevent library. Built on top of native async calls (select, poll, etc — the original async ), libevent nicely leverages a lot of low-level async functionality.

Working with gevent is fairly straightforward. One slight sticking point is thegevent.monkey.patch_all()which patches a lot of the standard library to cooperate better with gevent’s asychrony. It sounds scary, but I have yet to have a problem with the monkey patched implementations.

Without further ado, here’s how you use gevents to do parallel Solr requests:

import requests from gevent import monkey import gevent monkey.patch_all()class Searcher(object):""" Simple wrapper for doing a search and collecting theresults """def __init__(self, searchUrl):self.searchUrl = searchUrldef 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 taskhandles = []for searcher in searchers:handles.append(gevent.spawn(searcher.search))# Block until all work is donegevent.joinall(handles)# Dump the resultsfor searcher in searchers:print "Search Results for %s" % searcher.searchUrlfor 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) Lots more code, and not nearly as pretty as the equivalent Javascript, but it gets the job done. The meat of the code is these lines: # 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) We tell gevent to spawnsearcher.search. This gives us a handle to the spawned task. We can then optionally wait for all the spawned tasks to complete, then dump the results.

That’s about it! As always, comment if you have any thoughts on pointers. And let us know how we can help with any part of your Solr search application!

轉載于:https://my.oschina.net/letiantian/blog/323933

總結

以上是生活随笔為你收集整理的Async Solr Queries in Python的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。