- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章在Python的gevent框架下执行异步的Solr查询的教程由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
我经常需要用Python与solr进行异步请求工作。这里有段代码阻塞在Solr http请求上, 直到第一个完成才会执行第二个请求,代码如下: 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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请求: 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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代码简洁,但是它能完成相应的工作,代码的精髓是下面几行: 。
1
2
3
4
5
6
7
|
# 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搜索应用提供帮助.
最后此篇关于在Python的gevent框架下执行异步的Solr查询的教程的文章就讲到这里了,如果你想了解更多关于在Python的gevent框架下执行异步的Solr查询的教程的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我的图书馆是否有任何“官方”方式可以测试 gevent monkey 补丁是否已激活? 最佳答案 我不知道官方方法,但这是一种方法。 注意 os.fork 在猴子修补时发生变化: >>> import
存储在 threading.local 中的数据对于特定的协同例程来说是唯一的,还是对于 Python 线程来说仍然是唯一的? 最佳答案 根据 gevent.monkey 的文档 http://www
经过一整天的黑客攻击并试图找出问题所在后,我问了这个问题。 我想使用 requests 包发送请求,并以非阻塞模式进行。 为此,我使用了很好的 gevent 包及其猴子修补功能,我已经使用它很长时间了
我正在把我的头发拉到这个上面。我正在尝试使用 zeromq 和 gevent 获取最简单的示例。我将此脚本更改为使用 PUB/SUB 套接字,当我运行它时,“服务器”套接字将永远循环。如果我取消注释
我正在使用 Flask 和 Flask-SocketIO 构建 Web 接口(interface)/数据 API 以进行 websocket 通信。我想开始转向使用 Gevent/Gevent-web
假设 tasks 是 Greenlet 对象的列表。现在有什么区别 gevent.wait(tasks) 和 gevent.joinall(tasks) ? 最佳答案 不多! joinall 实际上在
我有一个使用线程的 python bottle 应用程序。因为我正在使用 monkey.patch ,线程正在阻止应用程序执行(从线程触发的对话框阻止瓶路由响应客户端,直到被关闭。) 这里的一项小研究
在 gevent monkey 补丁中,我没有看到任何关于默认文件对象的操作。如何在基于 gevent 的程序中使用异步文件读/写? 最佳答案 您可以使用 1.0b3 中可用的 gevent 的 fi
过去几天,我一直在尝试将事件流集成到我的 Flask 应用程序中,在我的本地测试中取得了不错的结果,但在我的服务器上使用 uWSGI 运行该应用程序时却有些糟糕。我的代码基本上建立在 example
来自 gevent 文档: The greenlets all run in the same OS thread and are scheduled cooperatively. 那么是否仍然需要使
我在 Gevent 池中执行几十个 HTTP 请求。 目标是在请求失败时重试一次,但仅重试一次。否则,它应该抛出异常。 我如何使用 at pool 编写 gevent 代码以支持在失败时重新运行一次
简而言之,我的问题是:我怎么知道某个特定的 Python 包何时与 gevent 兼容(至少与 gevent.monkey.patch_all 兼容)? 其实我想知道是否http-parser库与 g
为什么 gevent 会抛出这个错误?在 ipython、ubuntu 13 中运行它 In [1]: from gevent import monkey In [2]: monkey.patch_a
在我基于 gevent 的程序中,我在某处有一个线程,它陷入了一个循环,类似于: while True: gevent.sleep(0) 我怎么知道这是哪个线程?是否可以列出(并获取堆栈跟踪)
运行 gevent 的 WSGIServer 时,我遇到了一些非常奇怪的行为。似乎每个通过的请求的方法都被错误地解释了.. 如果我发送以下请求: requests.get('http://localh
我的项目正在使用 gevnet(这对我来说是新的)来实现 Thrift 服务器。 我正在阅读代码并从其文档中学习。下面的代码片段在我的项目中: TSocket.socket = gevent.sock
我尝试在我的 osX (el capitan) 上安装 Odoo。然后我下载了这个包,当我尝试使用 pip install -r requirements.txt 安装 3rf party lib 时
我有多个 greenlets 在一个公共(public)套接字上发送。是否保证通过 socket.sendall 发送的每个包都很好地分开,或者我是否必须在每次调用 sendall 之前获取锁。 所以
我刚开始使用 virtualenv,但我正在尝试安装 gevent在 virtualenv 环境中(我正在运行 Windows)。当我在 virtualenv 中使用 PIP 时,出现此错误: MyE
目前我正在使用带有发布-订阅模式的 zeromq,我有一个工作人员要发布和许多 (8) 个订阅者(都将订阅)相同的模式。 现在我尝试使用多处理来生成订阅者,它可以正常工作。我缺少一些消息。 我使用多处
我是一名优秀的程序员,十分优秀!