tornado 中的阻塞求指教

2014-03-30 19:49:49 +08:00
 nilai
如下代码:
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import time
import os

from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)

class IndexHandler(tornado.web.RequestHandler):
def get(self):
print "begin"
#time.sleep(10)
os.system("ping www.baidu.com -c 10")

if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/sleep", IndexHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()




现在当浏览器请求地址:
http://127.0.0.1:8000/sleep 会一直ping 10次百度,此时有另一个用户也访问这个地址, 但是另一个用户会被阻塞掉,一直等到第一个用户ping执行完后 第二个用户才能执行, 怎么样实现非阻塞的操作.
5171 次点击
所在节点    Python
27 条回复
ponyfk
2014-03-30 20:37:00 +08:00
看这个, 结合 subprocess
http://tornadogists.org/489093/
但是不推荐
zenliver
2014-03-30 20:50:36 +08:00
开个进程池,仍进去
yakiang
2014-03-30 21:32:06 +08:00
openroc
2014-03-30 22:19:04 +08:00
@nilai, search tornado.concurrent.run_on_executor
zenliver
2014-03-30 22:46:17 +08:00
@yakiang 曼城,,,
pythonee
2014-03-30 22:53:21 +08:00
啊,tornado处理请求不是多线程的吗?不是tornado用户,请别喷
yakiang
2014-03-30 22:57:02 +08:00
@zenliver 蓝粉 ^ ^ 这是我多个社交帐号的头像
nilai
2014-03-30 23:01:30 +08:00
Twisted 呢,能实现这个功能么?
wangfengmadking
2014-03-30 23:21:08 +08:00
加异步 coroutine
zenliver
2014-03-31 00:56:15 +08:00
@wangfengmadking 线程都阻塞了,加异步没作用的, 本来tornado就不适合cpu bound的应用
ivanlw
2014-03-31 08:36:08 +08:00
@zenliver 什么叫CPU bound呢
nilai
2014-03-31 09:35:55 +08:00
@zenliver 那是不是python就干不了这事了?
zenliver
2014-03-31 09:56:36 +08:00
@ivanlw cpu密集型
CMGS
2014-03-31 10:09:43 +08:00
等等,CPU密集?
time.sleep也好,os.system这种也好,前者直接 yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 10)这样加入事件循环,后者异步subprocess,tornado里面有tornado.process。

你是一个大业务循环一百亿次那才叫CPU密集,这只能算IO阻塞,但问题是这样的业务对于单进程单线程的事件驱动模型来说根本就是无解。要解决这种事无外乎就是stackless,线程池这种了,对于传统的CPython而言,只能用进程来解决。
kingxsp
2014-03-31 11:58:47 +08:00
查看这里: https://gist.github.com/kingxsp/9884962 应该是个可用的改写。
nilai
2014-03-31 14:37:25 +08:00
@kingxsp python2.7下没有 tornado.concurrent
kingxsp
2014-03-31 15:10:39 +08:00
这个并发库在python3自带在python2需要安装sudo pip install futures 即可使用
kingxsp
2014-03-31 15:12:14 +08:00
tornado.concurrent 是有的啊。我就是在python2.7下进行的,到是 futures 是 python3 下默认带的。
nilai
2014-03-31 16:01:45 +08:00
@kingxsp


root@xxxx-All-Series:~# ipython
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
Type "copyright", "credits" or "license" for more information.

IPython 0.12.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: import tornado.concurrent
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/home/yzy/<ipython-input-1-6952f9510533> in <module>()
----> 1 import tornado.concurrent

ImportError: No module named concurrent
nilai
2014-03-31 16:18:11 +08:00
@kingxsp 问题解决了. 是因为tornado 用的apt-get安装的.后来下源码安装就没问题. thanks

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/106539

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX