今回は、「どうせ記憶するなら全数記憶してみればいいんじゃないか?」という発想のもと、処理速度改善を行ってみます。
Continue reading 処理速度を向上させる その3 そして速度向上へ.
# tar tornado-0.1.tar.gz # cd tornado-0.1 # python setup.py install
$ python
Python 2.6.1 (r261:67515, Mar 26 2009, 15:57:10)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tornado.httpserver
>>> import tornado.ioloop
>>> import tornado.web
>>>
>>> class MainHandler(tornado.web.RequestHandler):
... def get(self):
... self.write("HELO\n")
...
>>> application = tornado.web.Application([(r"/", MainHandler),])
>>>
>>> http_server = tornado.httpserver.HTTPServer(application)
>>> http_server.listen(8888)
>>> tornado.ioloop.IOLoop.instance().start()
$ telnet localhost 8888 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. GET / HTTP/1.0 HTTP/1.0 200 OK Content-Length: 4 Etag: "50d7c437b1b17589574e811d5085ed34a4b22340" Content-Type: text/html; charset=UTF-8 Server: TornadoServer/0.1 HELO Connection closed by foreign host.
import sqlite3
# -*- coding: utf-8 -*-
con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE TEST(num integer)")
con.execute("INSERT INTO TEST(num) VALUES(200)")
for row in con.execute("SELECT * FROM TEST"):
print row[0]
con.close()
import sqlite
# -*- coding: utf-8 -*-
# RAM上にDBを作成
con = sqlite.connect(":memory:")
# 必要なテーブルを作成
cur = con.cursor()
cur.execute("create table TEST(num integer)")
# 値の書き込み
cur.execute("insert into TEST(num) values(300)")
# 値の参照
cur.execute("select * from TEST")
for row in cur:
print row[0]
# DBの削除
cur.close()
con.close()
こんにちは、しのはらです。
先日、オープンソースのサーバー仮想化管理ソフト「Karesansui」がリリースされました。
実はKaresansuiの裏では、HDEがリリースしたもうひとつのオープンソースソフトウェア 「Pysilhouette」が陰ながら動いてます。
今回は、オープンソースのバックグラウンドジョブマネージャー「Pysilhouette」をご紹介いたします。
# -*- coding: utf-8 -*-
from twisted.web import http, proxy
from twisted.internet import reactor, ssl
from twisted.python import log
import sys
import urlparse
# とりあえずログを標準出力へ
log.startLogging(sys.stdout)
# 上位プロキシ設定
proxyHost = "192.168.0.1"
proxyPort = 8080
# プロキシリクエストのクラス
class MyProxyRequest(proxy.ProxyRequest):
# processメソッドを継承
def process(self):
parsed = urlparse.urlparse(self.uri)
protocol = parsed[0]
host = parsed[1]
print self.method
port = self.ports[protocol]
if ':' in host:
host, port = host.split(':')
port = int(port)
rest = urlparse.urlunparse(('', '') + parsed[2:])
if not rest:
rest = rest + '/'
class_ = self.protocols[protocol]
headers = self.getAllHeaders().copy()
if 'host' not in headers:
headers['host'] = host
self.content.seek(0, 0)
s = self.content.read()
# 上位プロキシの設定がある場合はこちら
if proxyHost != "" and proxyPort != 0:
clientFactory = class_(self.method, self.uri, self.clientproto, headers, s, self)
self.reactor.connectTCP(proxyHost, proxyPort, clientFactory)
# プロキシが必要ない場合はこちら
else:
clientFactory = class_(self.method, rest, self.clientproto, headers, s, self)
self.reactor.connectTCP(host, port, clientFactory)
# プロキシクラスを継承
class MyProxy(proxy.Proxy):
requestFactory = MyProxyRequest
# HTTPプロキシサーバのクラス
class MyProxyFactory(http.HTTPFactory):
protocol = MyProxy
# 実行
reactor.listenTCP(8080, MyProxyFactory())
reactor.run
# -*- coding: utf-8 -*- from twisted.web import http, proxy from twisted.internet import reactor, ssl from twisted.python import log import sys # とりあえずログを標準出力へ log.startLogging(sys.stdout) # HTTPプロキシサーバのクラス class MyProxyFactory(http.HTTPFactory): protocol = proxy.Proxy # 実行 reactor.listenTCP(8080, MyProxyFactory()) reactor.run