본문 바로가기
파이썬

tornado python 알아가기..

by 혜룐 2015. 12. 16.

토네이도 http://www.tornadoweb.org/


특징은?

nginx > apache + Django 성능의 4배

단일스레드로 운영되고, 코어수만큼의 프로세스를 사용 (GIL 때문)

webFramework.. write(), finish(), flush()는 requestMainHandler안에서 호출해야 한다.

Asynchronous networking

tornado.ioloop — Main event loop

tornado.iostream — Convenient wrappers for non-blocking sockets

tornado.netutil — Miscellaneous network utilities

tornado.tcpclient  IOStream connection factory

tornado.tcpserver — Basic IOStream-based TCP server

web framework 

tornado.web  RequestHandler and Application classes

tornado.template — Flexible output generation

tornado.escape — Escaping and string manipulation

tornado.locale — Internationalization support

tornado.websocket — Bidirectional communication to the browser

httpServer and client

tornado.httpserver — Non-blocking HTTP server

tornado.httpclient — Asynchronous HTTP client

tornado.httputil — Manipulate HTTP headers and URLs

tornado.http1connection – HTTP/1.x client/server implementation

couroutines and cocurrency

tornado.gen  Simplify asynchronous code

tornado.concurrent — Work with threads and futures

tornado.locks – Synchronization primitives

tornado.queues – Queues for coroutines

tornado.process — Utilities for multiple processes

Integration with other services

tornado.auth — Third-party login with OpenID and OAuth

tornado.wsgi — Interoperability with other Python frameworks and servers

tornado.platform.asyncio — Bridge between asyncio and Tornado

tornado.platform.caresresolver — Asynchronous DNS Resolver using C-Ares

tornado.platform.twisted — Bridges between Twisted and Tornado

 이외 Utilites

tornado.autoreload — Automatically detect code changes in development

tornado.log — Logging support

tornado.options — Command-line parsing

tornado.stack_context — Exception handling across asynchronous callbacks

tornado.testing — Unit testing support for asynchronous code

tornado.util — General-purpose utilities



asyncio? 파이썬 3.4에 새로 추가된 라이브러리로, 파이썬에서 비동기  IO를 통해 조금 더 효율적으로 동시에 코드를 돌릴수 있게 해주는 라이브러리다. ( 3.3에선 pip install asyncio )


future?

제너레이터함수를 비동기로 실행하고, 결과를 보장해 주는 객체이다.

 


web framework 해보자!

By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.


1) virtualenv 환경 설정 + pip install 

catherineui-MacBook-Pro-2:tornado catherine$ virtualenv helloworld

New python executable in helloworld/bin/python

Installing setuptools, pip...done.

catherineui-MacBook-Pro-2:tornado catherine$ cd helloworld/

catherineui-MacBook-Pro-2:helloworld catherine$ ll

total 0

drwxr-xr-x  14 catherine  staff  476 12 16 11:28 bin

drwxr-xr-x   3 catherine  staff  102 12 16 11:27 include

drwxr-xr-x   3 catherine  staff  102 12 16 11:27 lib

catherineui-MacBook-Pro-2:helloworld catherine$ . bin/activate

(helloworld)catherineui-MacBook-Pro-2:helloworld catherine$ pip install tornado

/Users/catherine/Study/Python/tornado/helloworld/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.

  InsecurePlatformWarning

You are using pip version 6.1.1, however version 7.1.2 is available.

You should consider upgrading via the 'pip install --upgrade pip' command.

Collecting tornado

/Users/catherine/Study/Python/tornado/helloworld/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.

  InsecurePlatformWarning

  Downloading tornado-4.3.tar.gz (450kB)

    100% |████████████████████████████████| 454kB 216kB/s

Collecting backports.ssl-match-hostname (from tornado)

  Downloading backports.ssl_match_hostname-3.4.0.2.tar.gz

Collecting singledispatch (from tornado)

  Downloading singledispatch-3.4.0.3-py2.py3-none-any.whl

Collecting certifi (from tornado)

  Downloading certifi-2015.11.20.1-py2.py3-none-any.whl (368kB)

    100% |████████████████████████████████| 372kB 221kB/s

Collecting backports-abc>=0.4 (from tornado)

  Downloading backports_abc-0.4-py2.py3-none-any.whl

Collecting six (from singledispatch->tornado)

  Using cached six-1.10.0-py2.py3-none-any.whl

Installing collected packages: backports.ssl-match-hostname, six, singledispatch, certifi, backports-abc, tornado

  Running setup.py install for backports.ssl-match-hostname

  Running setup.py install for tornado

Successfully installed backports-abc-0.4 backports.ssl-match-hostname-3.4.0.2 certifi-2015.11.20.1 singledispatch-3.4.0.3 six-1.10.0 tornado-4.3


(helloworld)catherineui-MacBook-Pro-2:helloworld catherine$ pip list

You are using pip version 6.1.1, however version 7.1.2 is available.

You should consider upgrading via the 'pip install --upgrade pip' command.

backports-abc (0.4)

backports.ssl-match-hostname (3.4.0.2)

certifi (2015.11.20.1)

pip (6.1.1)

setuptools (15.0)

singledispatch (3.4.0.3)

six (1.10.0)

tornado (4.3)


2) inteliJ에서 interpreter 를 맞춰주고 

3) https://github.com/lhr0916/tornado_hello



참고

http://www.flowdas.com/blog/%ED%86%A0%EB%84%A4%EC%9D%B4%EB%8F%84tornado-%ED%94%84%EB%9E%9C%EB%93%9C%ED%94%BC%EB%93%9C%EC%9D%98-%EC%9B%B9-%EC%84%9C%EB%B2%84/

https://github.com/bootandy/tornado_sample/tree/master/sample