gevent简介2

本文介绍了从源代码安装gevent时遇到的常见问题及其解决方案,并提供了一个使用gevent进行并发任务处理的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 thenoexec option. This can cause a standard pip install gevent to fail with an error like cannot run C compiled programs. One fix is to mount the temporary directory without that option. Another may be to use the --build option to pip 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 valueproperty. 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 socketmodule 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

 

转载于:https://my.oschina.net/fdayok/blog/786104

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值