class SyncHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
# 耗时的代码
os.system("ping -c 2 www.google.com")
self.finish('It works')
ab -c 5 -n 5 http://127.0.0.1:5000/sync
Server Software: TornadoServer/4.3 Server Hostname: 127.0.0.1 Server Port: 5000 Document Path: /sync Document Length: 5 bytes Concurrency Level: 5 Time taken for tests: 5.076 seconds Complete requests: 5 Failed requests: 0 Total transferred: 985 bytes HTML transferred: 25 bytes Requests per second: 0.99 [#/sec] (mean) Time per request: 5076.015 [ms] (mean) Time per request: 1015.203 [ms] (mean, across all concurrent requests) Transfer rate: 0.19 [Kbytes/sec] received
class AsyncHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self, *args, **kwargs):
tornado.ioloop.IOLoop.instance().add_timeout(1, callback=functools.partial(self.ping, 'www.google.com'))
# do something others
self.finish('It works')
@tornado.gen.coroutine
def ping(self, url):
os.system("ping -c 2 {}".format(url))
return 'after'
Document Path: /async Document Length: 5 bytes Concurrency Level: 5 Time taken for tests: 0.009 seconds Complete requests: 5 Failed requests: 0 Total transferred: 985 bytes HTML transferred: 25 bytes Requests per second: 556.92 [#/sec] (mean) Time per request: 8.978 [ms] (mean) Time per request: 1.796 [ms] (mean, across all concurrent requests) Transfer rate: 107.14 [Kbytes/sec] received
class AsyncTaskHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self, *args, **kwargs):
# yield 结果
response = yield tornado.gen.Task(self.ping, ' www.google.com')
print 'response', response
self.finish('hello')
@tornado.gen.coroutine
def ping(self, url):
os.system("ping -c 2 {}".format(url))
return 'after'
Server Software: TornadoServer/4.3 Server Hostname: 127.0.0.1 Server Port: 5000 Document Path: /async/task Document Length: 5 bytes Concurrency Level: 5 Time taken for tests: 0.049 seconds Complete requests: 5 Failed requests: 0 Total transferred: 985 bytes HTML transferred: 25 bytes Requests per second: 101.39 [#/sec] (mean) Time per request: 49.314 [ms] (mean) Time per request: 9.863 [ms] (mean, across all concurrent requests) Transfer rate: 19.51 [Kbytes/sec] received
from concurrent.futures import ThreadPoolExecutor
class FutureHandler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(10)
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self, *args, **kwargs):
url = 'www.google.com'
tornado.ioloop.IOLoop.instance().add_callback(functools.partial(self.ping, url))
self.finish('It works')
@tornado.concurrent.run_on_executor
def ping(self, url):
os.system("ping -c 2 {}".format(url))
Document Path: /future Document Length: 5 bytes Concurrency Level: 5 Time taken for tests: 0.003 seconds Complete requests: 5 Failed requests: 0 Total transferred: 995 bytes HTML transferred: 25 bytes Requests per second: 1912.78 [#/sec] (mean) Time per request: 2.614 [ms] (mean) Time per request: 0.523 [ms] (mean, across all concurrent requests) Transfer rate: 371.72 [Kbytes/sec] received
class Executor(ThreadPoolExecutor):
_instance = None
def __new__(cls, *args, **kwargs):
if not getattr(cls, '_instance', None):
cls._instance = ThreadPoolExecutor(max_workers=10)
return cls._instance
class FutureResponseHandler(tornado.web.RequestHandler):
executor = Executor()
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self, *args, **kwargs):
future = Executor().submit(self.ping, 'www.google.com')
response = yield tornado.gen.with_timeout(datetime.timedelta(10), future,
quiet_exceptions=tornado.gen.TimeoutError)
if response:
print 'response', response.result()
@tornado.concurrent.run_on_executor
def ping(self, url):
os.system("ping -c 1 {}".format(url))
return 'after'
Concurrency Level: 5 Time taken for tests: 0.043 seconds Complete requests: 5 Failed requests: 0 Total transferred: 960 bytes HTML transferred: 0 bytes Requests per second: 116.38 [#/sec] (mean) Time per request: 42.961 [ms] (mean) Time per request: 8.592 [ms] (mean, across all concurrent requests) Transfer rate: 21.82 [Kbytes/sec] received
class SyncHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
url = 'https://api.github.com/'
resp = requests.get(url)
print resp.status_code
self.finish('It works')
Document Path: /sync Document Length: 5 bytes Concurrency Level: 5 Time taken for tests: 10.255 seconds Complete requests: 5 Failed requests: 0 Total transferred: 985 bytes HTML transferred: 25 bytes Requests per second: 0.49 [#/sec] (mean) Time per request: 10255.051 [ms] (mean) Time per request: 2051.010 [ms] (mean, across all concurrent requests) Transfer rate: 0.09 [Kbytes/sec] received
class AsyncHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self, *args, **kwargs):
url = 'https://api.github.com/'
http_client = tornado.httpclient.AsyncHTTPClient()
http_client.fetch(url, self.on_response)
self.finish('It works')
@tornado.gen.coroutine
def on_response(self, response):
print response.code
Document Path: /async Document Length: 5 bytes Concurrency Level: 5 Time taken for tests: 0.162 seconds Complete requests: 5 Failed requests: 0 Total transferred: 985 bytes HTML transferred: 25 bytes Requests per second: 30.92 [#/sec] (mean) Time per request: 161.714 [ms] (mean) Time per request: 32.343 [ms] (mean, across all concurrent requests) Transfer rate: 5.95 [Kbytes/sec] received
class AsyncResponseHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self, *args, **kwargs):
url = 'https://api.github.com/'
http_client = tornado.httpclient.AsyncHTTPClient()
response = yield tornado.gen.Task(http_client.fetch, url)
print response.code
print response.body
headers = self.request.headers
body = json.dumps({'name': 'rsj217'})
http_client = tornado.httpclient.AsyncHTTPClient()
resp = yield tornado.gen.Task(
self.http_client.fetch,
url,
method="POST",
headers=headers,
body=body,
validate_cert=False)
body = urllib.urlencode(params) req = tornado.httpclient.HTTPRequest( url=url, method='POST', body=body, validate_cert=False) http_client.fetch(req, self.handler_response) def handler_response(self, response): print response.code
@router.Route('/api/v2/account/upload')
class ApiAccountUploadHandler(helper.BaseHandler):
@tornado.gen.coroutine
@helper.token_require
def post(self, *args, **kwargs):
upload_type = self.get_argument('type', None)
files_body = self.request.files['file']
new_file = 'upload/new_pic.jpg'
new_file_name = 'new_pic.jpg'
# 写入文件
with open(new_file, 'w') as w:
w.write(file_['body'])
logging.info('user {} upload {}'.format(user_id, new_file_name))
# 异步请求 上传图片
with open(new_file, 'rb') as f:
files = [('image', new_file_name, f.read())]
fields = (('api_key', KEY), ('api_secret', SECRET))
content_type, body = encode_multipart_formdata(fields, files)
headers = {"Content-Type": content_type, 'content-length': str(len(body))}
request = tornado.httpclient.HTTPRequest(config.OCR_HOST,
method="POST", headers=headers, body=body, validate_cert=False)
response = yield tornado.httpclient.AsyncHTTPClient().fetch(request)
def encode_multipart_formdata(fields, files):
"""
fields is a sequence of (name, value) elements for regular form fields.
files is a sequence of (name, filename, value) elements for data to be
uploaded as files.
Return (content_type, body) ready for httplib.HTTP instance
"""
boundary = '----------ThIs_Is_tHe_bouNdaRY_$'
crlf = '\r\n'
l = []
for (key, value) in fields:
l.append('--' + boundary)
l.append('Content-Disposition: form-data; name="%s"' % key)
l.append('')
l.append(value)
for (key, filename, value) in files:
filename = filename.encode("utf8")
l.append('--' + boundary)
l.append(
'Content-Disposition: form-data; name="%s"; filename="%s"' % (
key, filename
)
)
l.append('Content-Type: %s' % get_content_type(filename))
l.append('')
l.append(value)
l.append('--' + boundary + '--')
l.append('')
body = crlf.join(l)
content_type = 'multipart/form-data; boundary=%s' % boundary
return content_type, body
def get_content_type(filename):
import mimetypes
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
files = {}
f = open('/Users/ghost/Desktop/id.jpg')
files['image'] = f
data = dict(api_key='KEY', api_secret='SECRET')
resp = requests.post(url, data=data, files=files)
f.close()
print resp.status_Code
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有