Skip to content

Instantly share code, notes, and snippets.

View alejandrobernardis's full-sized avatar
🐻

Alejandro M. BERNARDIS alejandrobernardis

🐻
View GitHub Profile
from tornado import stack_context as old, new_stack as new
def exc(type, value, tb):
return True
def non_exc(type, value, tv):
pass
# For http://stackoverflow.com/questions/21248222/how-can-tornado-serve-a-single-static-file-at-an-arbitrary-location/21248691?noredirect=1#comment32053685_21248691
class MyFileHandler(StaticFileHandler):
def initialize(self, file_mapping, **kwargs):
self.file_mapping = file_mapping
super(MyFileHandler, self).initialize(**kwargs)
@classmethod
def get_absolute_path(cls, root, path):
return StaticFileHandler.get_absolute_path(root, self.file_mapping.get(path, path))
"""Proof of concept tornado/tulip integration.
Works with the current development branch of both projects as of
Jan 20. Current status: The tornado test suite passes cleanly
on a tulip-backed IOLoop. The tornado-backed event loop for tulip
supports the core call_later and add_reader method families, but not
the higher-level networking methods.
To run the tornado test suite on tulip, make sure both projects
and this file are on your python path and run:
from concurrent.futures import ThreadPoolExecutor
from functools import partial, wraps
import time
import tornado.ioloop
import tornado.web
EXECUTOR = ThreadPoolExecutor(max_workers=4)
@alejandrobernardis
alejandrobernardis / async_ses.py
Last active August 29, 2015 14:08
(Tornado Async) A little hack for boto.SESConnections
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Asumi Kamikaze Inc.
# Licensed under the MIT License.
# Author: Alejandro M. Bernardis
# Email: alejandro (dot) bernardis (at) asumikamikaze (dot) com
# Created: 06/Nov/2014 19:27
import boto
from boto import jsonresponse
@alejandrobernardis
alejandrobernardis / demo.py
Created November 8, 2014 20:29
Tornoado Gen: Test-1,999,991 ;)
import json
from tornado import gen, ioloop
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.web import HTTPError
AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
class CoroutineModel(object):
"""
Testing with coroutines and yielding futures
This programs final ouptput is 3
get_x, and get_y simulate an async task that returns later.
"""
import tornado.ioloop
from tornado.concurrent import Future
from tornado import gen
@alejandrobernardis
alejandrobernardis / start_server.py
Created November 7, 2014 15:28
Tornado, start server
# https://groups.google.com/forum/#!starred/python-tornado/VpHp3kXHP7s
def start_httpserver(app_root, handler_map, opts):
# create app and http server
app = Application(handler_map, app_root, opts)
http_server = tornado.httpserver.HTTPServer(app, xheaders=True)
ioloop = tornado.ioloop.IOLoop.instance()
def mem_check_scheduler():
easylog.debug("Call Check MEM")
@alejandrobernardis
alejandrobernardis / tornado.py
Last active August 29, 2015 14:08
Tornado Coroutines
from tornado import gen, ioloop
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.web import HTTPError
AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
class CoroutineModel(object):
@gen.coroutine
def send_ok(self):
try:
request = HTTPRequest('https://google.com')
@alejandrobernardis
alejandrobernardis / crypto.py
Created October 20, 2014 20:38
Simple Crypto
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Asumi Kamikaze Inc.
# Licensed under the MIT License.
# Author: Alejandro M. Bernardis
# Email: alejandro (dot) bernardis (at) asumikamikaze (dot) com
# Created: 20/Oct/2014 12:22
from base64 import encodestring, decodestring
from Crypto import Random