Skip to content

Instantly share code, notes, and snippets.

View alejandrobernardis's full-sized avatar
🐻

Alejandro M. BERNARDIS alejandrobernardis

🐻
View GitHub Profile
@alejandrobernardis
alejandrobernardis / gulpfile.js
Created December 14, 2014 20:24
Gulp + Stylus (watcher)
var gulp = require('gulp');
var stylus = require('gulp-stylus');
var browser_sync = require('browser-sync');
var reload = browser_sync.reload;
gulp.task('stylus', function() {
return gulp.src('./static/css/*.styl')
.pipe(stylus())
.pipe(gulp.dest('./static/css'))
@alejandrobernardis
alejandrobernardis / i.sh
Created December 12, 2014 17:12
CentOS 7
yum -y update \
&& yum -y upgrade \
&& yum -y groupinstall "Development tools" \
&& yum -y install nano wget tree vim \
&& rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
@alejandrobernardis
alejandrobernardis / test.py
Created December 4, 2014 22:16
Nested coroutines
from tornado import gen, ioloop
from tornado.httpclient import AsyncHTTPClient
@gen.coroutine
def worker():
sites = ['http://google.com', 'http://clarin.com', 'http://lanacion.com']
@gen.coroutine
def _bulk():
@alejandrobernardis
alejandrobernardis / result
Created November 28, 2014 00:59
dir vs hasattr
i: a
True
r: False
True
---
i: b
True
r: ""
True
---
@alejandrobernardis
alejandrobernardis / b7.py
Last active August 29, 2015 14:10
Mixpanel Async Client (tornado web server)
#!/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 11:59
import datetime
from random import randint
from collections import deque
MIXPANEL_EVENTS = 'events'
MIXPANEL_PEOPLE = 'people'
MIXPANEL_IMPORTS = 'imports'
MIXPANEL_ENDPOINTS = {
MIXPANEL_EVENTS: 'https://api.mixpanel.com/track',
from concurrent.futures import ThreadPoolExecutor
from tornado import gen
from tornado.process import cpu_count
import bcrypt
# global threadpool
pool = ThreadPoolExecutor(cpu_count())
@gen.coroutine
def create_user(name, password):
@alejandrobernardis
alejandrobernardis / crypto.py
Created November 10, 2014 21:18
Python, Crypto Methods (sync/async)
#!/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 concurrent.futures import ThreadPoolExecutor

Asynchronous programming with Tornado

Asynchronous programming can be tricky for beginners, therefore I think it's useful to iron some basic concepts to avoid common pitfalls.

For an explanation about generic asynchronous programming, I recommend you one of the [many][2] [resources][3] [online][4].

I will focus on solely on asynchronous programming in [Tornado][1]. From Tornado's homepage:

import concurrent.futures
class ProcessManager(object):
def __init__(self):
self.pool = concurrent.futures.ProcessPoolExecutor()
self.futures = {}
def submit(self, f, *args, **kwargs):