![](/img/trans.png)
[英]How to use multiple ioloop in tornado and share data between the ioloop(s)?
[英]Why is there a difference in performance between Tornado's ioloop and httpserver?
我決定將Python Tornado用作啟動時的選擇服務器,並且我針對兩個參考Python Tornado實現運行httpref來對Tornado的功能進行壓力測試。 這是我互相碰到的兩段代碼:
iostream:
import errno
import functools
import socket
from tornado import ioloop, iostream
def connection_ready(sock, fd, events):
while True:
try:
connection, address = sock.accept()
except socket.error, e:
if e[0] not in (errno.EWOULDBLOCK, errno.EAGAIN):
raise
return
connection.setblocking(0)
stream = iostream.IOStream(connection)
message = "You requested %s\n" % "hi"
stream.write("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s" % (len(message), message), stream.close)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(0)
sock.bind(("", 8011))
sock.listen(5000)
io_loop = ioloop.IOLoop.instance()
callback = functools.partial(connection_ready, sock)
io_loop.add_handler(sock.fileno(), callback, io_loop.READ)
try:
io_loop.start()
except KeyboardInterrupt:
io_loop.stop()
print "exited cleanly"
HTTPServer:
from tornado import httpserver
from tornado import ioloop
def handle_request(request):
message = "You requested %s\n" % "hi"
request.write("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s" % (len(message), message))
request.finish()
http_server = httpserver.HTTPServer(handle_request)
http_server.bind(8012)
http_server.start(0)
try:
ioloop=ioloop.IOLoop.instance()
ioloop.start()
except KeyboardInterrupt:
ioloop.stop()
(注意:第一個實現來自http://nichol.as/asynchronous-servers-in-python )
我擔心我從httperf --server=localhost --port=8011 --rate=4000 --num-conns=80000
:
iostream:
Reply rate [replies/s]: min 3852.3 avg 3973.1 max 4094.5 stddev 112.3 (4 samples)
Reply time [ms]: response 12.2 transfer 0.0
[...]
Errors: total 499 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 499 addrunavail 0 ftab-full 0 other 0
HTTPServer:
Reply rate [replies/s]: min 0.0 avg 1280.7 max 2138.5 stddev 962.8 (4 samples)
Reply time [ms]: response 334.6 transfer 0.0
[...]
Errors: total 51697 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 47569 addrunavail 0 ftab-full 0 other 4128
有人對iostream為什么比httpserver表現更好有一個很好的解釋嗎? 提前致謝!
錯誤:fd-unavail 47569
這意味着您的計算機沒有文件描述符。
您的httperf失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.