1
ponyfk 2014-03-30 20:37:00 +08:00
|
2
zenliver 2014-03-30 20:50:36 +08:00
开个进程池,仍进去
|
3
yakiang 2014-03-30 21:32:06 +08:00 1
|
6
pythonee 2014-03-30 22:53:21 +08:00
啊,tornado处理请求不是多线程的吗?不是tornado用户,请别喷
|
8
nilai OP Twisted 呢,能实现这个功能么?
|
9
wangfengmadking 2014-03-30 23:21:08 +08:00
加异步 coroutine
|
10
zenliver 2014-03-31 00:56:15 +08:00
@wangfengmadking 线程都阻塞了,加异步没作用的, 本来tornado就不适合cpu bound的应用
|
14
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而言,只能用进程来解决。 |
15
kingxsp 2014-03-31 11:58:47 +08:00 1
查看这里: https://gist.github.com/kingxsp/9884962 应该是个可用的改写。
|
17
kingxsp 2014-03-31 15:10:39 +08:00
这个并发库在python3自带在python2需要安装sudo pip install futures 即可使用
|
18
kingxsp 2014-03-31 15:12:14 +08:00
tornado.concurrent 是有的啊。我就是在python2.7下进行的,到是 futures 是 python3 下默认带的。
|
19
nilai OP @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 |
21
ggarlic 2014-03-31 23:01:57 +08:00
@nilai very easy
使用ProcessProtocol https://twistedmatrix.com/documents/13.0.0/core/howto/process.html 之前看到有人用twisted写爬虫用urlopen嫌慢,其实twisted也有异步的urlopen版本twisted.web.client.getPage 写异步用同步的time.sleep之类的不是自己给自己找麻烦么 |
24
CMGS 2014-04-01 01:01:23 +08:00
@zenliver time.sleep 其实比较特殊,为什么说特殊呢,原本是用来模拟大循环(业务向)的CPU密集操作,但是在event-driven的驱动里面,比喻起来应该是suspend,和时间循环是相关的
|
26
ggarlic 2014-04-01 22:17:24 +08:00
<script src="https://gist.github.com/ggarlic/f0d5c4135a98cce96c7a.js"></script>
在我这测试能用,请自行修改ping的路径。 然后,web开发我不熟,我拿twisted都是写别的东西。用同一个浏览器同时打开几个页面还是会顺序访问,用不同浏览器,或者直接终端里同时跑几个curl就没事。Twisted Network Programming Essentials, 2nd EditionEssential Twisted里关于这个的说明: If you run Example 4-10 and then load multiple instances of http://localhost:8000 in a browser, you may still find that the requests are processed serially. This is not Twisted’s fault: some browsers, notably Chrome, serialize requests to the same resource. You can verify that the web server isn’t blocking by issuing several simultaneous requests through cURL or a quick Python script. |
27
ggarlic 2014-04-01 22:21:48 +08:00
|