标签: greenlets

让Django变绿

我有一个Django管理命令,可以产生数千个TCP/UDP请求.我已经使用Gevent加快了速度,因为我重新组织了我的代码以作为协程工作.套接字连接不再阻塞,但从我所读到的,Django的部分仍然不是绿色.(绿色,我的意思是使用greenlets.)

你能告诉我Django的哪些部分不是绿色的,我可以做些什么让它们变绿?我认为有些DB相关的部分仍然存在阻碍.是否有任何Django库/补丁可以帮助我实现绿色?

我不太关心请求/响应周期是否为绿色,因此Gunicorn会帮助我吗?

谢谢

python django coroutine gevent greenlets

10
推荐指数
1
解决办法
2511
查看次数

通过HTTPS使用gevent/grequests进行奇怪的阻止行为

以下代码每200ms发送一次请求,并且应该在它们到来时异步处理响应.

通过HTTP,它按预期工作 - 每200ms发送一个请求,并在响应到达时独立调用响应回调.但是,通过HTTPS,只要响应到达,请求就会显着延迟(即使我的响应处理程序不起作用).对于每个请求,响应回调似乎被调用两次,一次是零长度响应(编辑:这是因为重定向并且似乎与阻塞问题无关,感谢Padraic).

什么可能导致这种阻止行为通过HTTPS?(www.bbc.co.uk这只是一个地理位置远离我的例子,但它发生在我测试过的所有服务器上).

grequests_test.py

import time
import sys
import grequests
import gevent

def cb(res, **kwargs):
    print("**** Response", time.time(), len(res.text))

for i in range(10):
    unsent = grequests.get(sys.argv[1], hooks={'response': cb})
    print("Request", time.time())
    grequests.send(unsent, grequests.Pool(1))
    gevent.sleep(0.2)
gevent.sleep(5)
Run Code Online (Sandbox Code Playgroud)

$ ipython2 grequests_test.py 'http://www.bbc.co.uk' (预期结果)

('Request', 1459050191.499266)
('Request', 1459050191.701466)
('Request', 1459050191.903223)
('Request', 1459050192.10403)
('Request', 1459050192.305626)
('**** Response', 1459050192.099185, 179643)
('Request', 1459050192.506476)
('**** Response', 1459050192.307869, 179643)
('Request', 1459050192.707745)
('**** Response', 1459050192.484711, 179643)
('Request', 1459050192.909376)
('**** Response', 1459050192.696583, 179643)
('Request', 1459050193.110528)
('**** Response', 1459050192.870476, …
Run Code Online (Sandbox Code Playgroud)

python https gevent greenlets grequests

9
推荐指数
1
解决办法
769
查看次数

我正在寻找适用于Windows的Python 2.7的gevent

然而,gevent依赖于greenlet和我发现无法安装的.msi版本,并且2.6的egg拒绝安装.

msi版本失败如下.

C:\Windows\system32>easy_install greenlet
install_dir C:\Python27\Lib\site-packages\
Searching for greenlet
Reading http://pypi.python.org/simple/greenlet/
Reading http://bitbucket.org/ambroff/greenlet
Reading http://undefined.org/python/#greenlet
Best match: greenlet 0.3.1
Downloading http://pypi.python.org/packages/source/g/greenlet/greenlet-0.3.1.tar.gz#md5=8d75d7f3f659e915e286e1b0fa0e1c4d
Processing greenlet-0.3.1.tar.gz
Running greenlet-0.3.1\setup.py -q bdist_egg --dist-dir c:\users\ian\appdata\local\temp\easy_install-1epg28\greenlet-0.3.1\egg-dist-tmp-mqhu3n
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'repository'
  warnings.warn(msg)
greenlet.c
greenlet.c : fatal error C1074: 'IDB' is illegal extension for PDB file:
error: Setup script exited with error: command '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\cl.exe"' failed with exit status 2

C:\Windows\system32>
Run Code Online (Sandbox Code Playgroud)

我想这意味着我有一个错误的VS版本.

还有什么我可以使用的吗?

我想得到PyQt4,因此如果无法访问数据库或需要很长时间才能回复,GUI将无法锁定.

python gevent greenlets

6
推荐指数
2
解决办法
8813
查看次数

如何在gevent中捕获回溯

我已经产生了一个Greenlet并将其链接到一个可调用的.一段时间后,Greenlet因异常而失败.链接的callable被调用.这一切都很棒!

这是问题所在:

正如您所期望的那样,我的控制台上会出现异常的回溯.但我想在链接的callable中使用该回溯做一些事情.如何在链接的callable中访问该回溯?

(我的第一直觉就是使用traceback.extract_stack(),但事实证明它为链接的callable本身而不是Exception提供了追溯.)

python exception-handling gevent greenlets

6
推荐指数
1
解决办法
2529
查看次数

Intel Galileo - greenlet.h:8:20:致命错误:Python.h:没有这样的文件或目录

我在我的intel galileo上运行Linux dev-tools图像(最后链接).我试图安装greenlet但是我收到一个错误,说明python.h没有这样的文件.

root@clanton:/media/realroot/greenlet-0.4.2# python setup.py install
running install
running build
running build_ext
creating /tmp/tmpuKbWhk/tmp
creating /tmp/tmpuKbWhk/tmp/tmpuKbWhk
i586-poky-linux-uclibc-gcc -m32 -march=i586 -fno-strict-aliasing -O2 -pipe -g -feliminate-unused-debug-types -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -fno-tree-dominator-opts -I/usr/include/python2.7 -c /tmp/tmpuKbWhk/simple.c -o /tmp/tmpuKbWhk/tmp/tmpuKbWhk/simple.o
/tmp/tmpuKbWhk/simple.c:1:6: warning: function declaration isn't a prototype [-Wstrict-prototypes]
building 'greenlet' extension
creating build
creating build/temp.linux-i586-2.7
i586-poky-linux-uclibc-gcc -m32 -march=i586 -fno-strict-aliasing -O2 -pipe -g -feliminate-unused-debug-types -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -fno-tree-dominator-opts -I/usr/include/python2.7 -c greenlet.c -o build/temp.linux-i586-2.7/greenlet.o
Run Code Online (Sandbox Code Playgroud)

在greenlet.c中包含的文件中:5:0:

greenlet.h:8:20: fatal error: Python.h: No …
Run Code Online (Sandbox Code Playgroud)

python linux gcc greenlets intel-galileo

6
推荐指数
0
解决办法
2812
查看次数

使用gevent下载图像

我的任务是从给定的网址列表中下载1M +图像.建议的方法是什么?

阅读Greenlet Vs. 我调查过的线程gevent,但是我无法可靠地运行它.我玩了100个网址的测试集,有时它在1.5秒内完成,但有时它需要超过30秒,这很奇怪,因为每个请求的超时*为0.1,所以它永远不会超过10秒.

*见下面的代码

我也调查过,grequests但他们似乎有异常处理问题.

我的"要求"就是我能做到的

  • 检查下载时出现的错误(超时,损坏的图像......),
  • 监控已处理图像的数量和进度
  • 尽可能快.
from gevent import monkey; monkey.patch_all()
from time import time
import requests
from PIL import Image
import cStringIO
import gevent.hub
POOL_SIZE = 300


def download_image_wrapper(task):
    return download_image(task[0], task[1])

def download_image(image_url, download_path):
    raw_binary_request = requests.get(image_url, timeout=0.1).content
    image = Image.open(cStringIO.StringIO(raw_binary_request))
    image.save(download_path)

def download_images_gevent_spawn(list_of_image_urls, base_folder):
    download_paths = ['/'.join([base_folder, url.split('/')[-1]])
                      for url in list_of_image_urls]
    parameters = [[image_url, download_path] for image_url, download_path in
             zip(list_of_image_urls, download_paths)]
    tasks = [gevent.spawn(download_image_wrapper, …
Run Code Online (Sandbox Code Playgroud)

python concurrency gevent greenlets

6
推荐指数
1
解决办法
915
查看次数

问题理解警告:在/ usr/local/include中找到了未提取的头文件

运行后brew doctor,我得到与我不确定的头文件相关的错误.我不知道是什么greenlet,所以我很担心按照这篇文章的建议删除它.

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected header files:
    /usr/local/include/python2.7/greenlet/greenlet.h
Run Code Online (Sandbox Code Playgroud)

请回答以下问题:

1.错误Unbrewed header files究竟意味着什么?

2.我应该删除错误中列出的文件吗?

python homebrew python-2.7 greenlets

6
推荐指数
2
解决办法
5591
查看次数

gunicorn + gevent 猴子会自动将 threadlocals 修补为 greenlet-locals 吗?

在 gevent 模式下运行 gunicorn 时,是否已经自动应用了此处描述的使 threadlocals greenlet-locals的猴子补丁(特别是用于 threadlocals 的补丁)

(我正在运行 django,目前使用 threadlocals 对大型查询结果进行一些快速缓存——我知道 gevent/greenlet 使用传统线程的替代模型,因此我很担心)。

python django gevent greenlets gunicorn

5
推荐指数
1
解决办法
1054
查看次数

此事件已被另一个 greenlet 使用

我使用 gunicorn 17.5 with worker_class="gevent", workers=3,我从gunicorn -c config.py my:app. 我刚刚注意到我的日志中有以下错误

2014-04-01 04:48:49 [4297] [ERROR] Error handling request
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/gunicorn/workers/async.py", line 45, in handle
    self.handle_request(listener, req, client, addr)
  File "/usr/lib/python2.6/site-packages/gunicorn/workers/ggevent.py", line 119, in handle_request
    super(GeventWorker, self).handle_request(*args)
  File "/usr/lib/python2.6/site-packages/gunicorn/workers/async.py", line 93, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/usr/lib/python2.6/site-packages/beaker/middleware.py", line 155, in __call__
    return self.wrap_app(environ, session_start_response)
  [...]
  File "/usr/lib/python2.6/site-packages/mysql/connector/connection.py", line 1083, in cursor
    if not self.is_connected():
  File "/usr/lib/python2.6/site-packages/mysql/connector/connection.py", line 696, in is_connected
    self.cmd_ping()
  File …
Run Code Online (Sandbox Code Playgroud)

python mysql greenlets gunicorn

5
推荐指数
0
解决办法
1306
查看次数

具有 2 个工作线程和 2 个线程的 Gunicorn config.py 文件仅并行处理 2 个请求

原始github问题中没有答案: https://github.com/tiangolo/meinheld-gunicorn-flask-docker/issues/48

在下面的简单设置中,我期望 Gunicorn 能够并行处理至少 4 个请求,但我只得到 2 个请求。在下面的配置中,我们可以看到 2 个工作线程和 2 个线程。

此外,我们尝试使用 2 个线程配置工作人员数量 == 1,然后我们在给定时刻仅处理 1 个请求。所以看起来,gunicorn + meinheld 设置只能在每个工作进程中处理 1 个请求,这是非常低效的。

我们在每个设置中并行发送 53 个请求。

simple_app.py:

import sys

from flask import Flask, request
import json
import time

app = Flask(__name__)


@app.route("/", methods=['POST'])
def hello():
    headers = request.headers
    data = json.loads(request.data)
    version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
    message = "Hello World from Flask in a Docker container running Python {} with Meinheld and Gunicorn (default): {}:{}".format(
        version, headers, json.dumps(data)
    ) …
Run Code Online (Sandbox Code Playgroud)

python gevent flask greenlets gunicorn

5
推荐指数
0
解决办法
1727
查看次数