Created
August 1, 2012 20:05
-
-
Save vortec/3230237 to your computer and use it in GitHub Desktop.
Heavy calculation using multiple core and Tornado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import random | |
from time import time | |
def vectable(runs=25000): | |
""" """ | |
test = [] | |
for i in range(0, 1024): | |
x, y, z, w = random(), random(), random(), random() | |
test.append((x, y, z, w)) | |
start = time() | |
total = 0 | |
for i in range(0, runs): | |
for j in range(0, 1023): | |
#print i, j | |
a, b = test[j], test[j+1] | |
dot = (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]) + (a[3] * b[3]) | |
total = total + dot | |
finish = time() | |
result = 'table %.2fs total: %f' % ((finish-start), total) | |
return result | |
import logging | |
import tornado.ioloop | |
import tornado.web | |
from tornado.process import fork_processes, task_id | |
from tornado.options import define, options | |
import tornado.autoreload | |
import tornado.httpserver | |
import sys | |
class MainHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write(vectable()) | |
application = tornado.web.Application([ | |
(r"/", MainHandler), | |
]) | |
if __name__ == '__main__': | |
define("port", default = 9000, help = "run on the given port", type = int) | |
define("debug", default = False, help = "run in debug mode", type = bool) | |
config = sys.argv[-1] | |
#options.parse_config_file(config) | |
options.parse_command_line() | |
if not options.debug: | |
fork_processes(None) | |
options.port += task_id() or 0 | |
logging.info('Starting Tornado on port %i' % options.port) | |
tornado.httpserver.HTTPServer(application).listen(options.port, '0.0.0.0') | |
ioloop = tornado.ioloop.IOLoop.instance() | |
ioloop.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment