Common Installation Issues安装常见问题
The following are some common installation problems and solutions for those compiling gevent from source. 下面是从代码编译安装gevent常见问题和解决方案.
- Some Linux distributions are now mounting their temporary directories with the
noexec
option. This can cause a standardpip install gevent
to fail with an error likecannot run C compiled programs
. One fix is to mount the temporary directory without that option. Another may be to use the--build
option topip install
to specify another directory. See issue #570 and issue #612 for examples. 一些linux的发行版现在挂载临时目录使用了noexec选项.这会导致用pip install gevent报一个类似"can not run C compiled programs."的错. 解决方案之一是挂载临时目录时去掉noexec选项.另一个是使用--build选项指定一个目录给pip install. - Also check for conflicts with environment variables like
CFLAGS
. For example, seeLibrary Updates. 也检查下环境变量冲突,如CFLAGS. - Users of a recent SmartOS release may need to customize the
CPPFLAGS
(the environment variable containing the default options for the C preprocessor) if they are using the libev shipped with gevent. See Operating Systems for more information. SmartOS最近版本的用户可能需要自定义CPPFLAGS
(一个包含了C预处理器默认选项的环境变量),如果使用了被gevent依赖的libev.
Example
The following example shows how to run tasks concurrently.
>>> import gevent
>>> from gevent import socket
>>> urls = ['www.google.com', 'www.example.com', 'www.python.org']
>>> jobs = [gevent.spawn(socket.gethostbyname, url) for url in urls]
>>> gevent.joinall(jobs, timeout=2)
>>> [job.value for job in jobs]
['74.125.79.106', '208.77.188.166', '82.94.164.162']
After the jobs have been spawned, gevent.joinall()
waits for them to complete, allowing up to 2 seconds. The results are then collected by checking the value
property. The gevent.socket.gethostbyname()
function has the same interface as the standard socket.gethostbyname()
but it does not block the whole interpreter and thus lets the other greenlets proceed with their requests unhindered.
当jobs被spawned后, gevent.joinall()
将等待他们结束,最多等待2秒.
Monkey patching
The example above used gevent.socket
for socket operations. If the standard socket
module was used the example would have taken 3 times longer to complete because the DNS requests would be sequential (serialized). Using the standard socket module inside greenlets makes gevent rather pointless, so what about existing modules and packages that are built on top of socket
(including the standard library modules likeurllib
)?
That’s where monkey patching comes in. The functions in gevent.monkey
carefully replace functions and classes in the standard socket
module with their cooperative counterparts. That way even the modules that are unaware of gevent can benefit from running in a multi-greenlet environment.
>>> from gevent import monkey; monkey.patch_socket()
>>> import urllib2 # it's usable from multiple greenlets now
See examples/concurrent_download.py