Skip to content

Instantly share code, notes, and snippets.

@litnimax
litnimax / gist:012569768c0e2210021b651e5ccd0e09
Created July 28, 2016 08:51 — forked from tmc/gist:777085
multiprocessing gevent chat example
import sys
import gevent
from gevent.monkey import patch_all; patch_all()
from gevent import server, event, socket
from multiprocessing import Process, current_process, cpu_count
"""
Simple multiprocess StreamServer that proxies messages between clients.
Avoids using a multiprocessing.Event since it blocks on a semaphore.
@litnimax
litnimax / zmq_socket.py
Created March 8, 2016 10:11 — forked from anonymous/gist:bc82a0bea35a5cb3ce01
ZMQ Socket with timeout
from functools import update_wrapper
import zmq
class Socket(zmq.Socket):
def __init__(self, ctx, type, default_timeout=None):
zmq.Socket.__init__(self, ctx, type)
self.default_timeout = default_timeout
def on_timeout(self):
## credit: http://fabian-affolter.ch/blog/the-lineinfile-module-of-ansible/
---
- hosts: alpine_install
user: root
tasks:
# - name: create a complete empty file
# command: /usr/bin/touch /test/test.conf
- name: create a new file with lineinfile
@litnimax
litnimax / pedantically_commented_playbook.yml
Created February 10, 2016 15:23 — forked from phred/pedantically_commented_playbook.yml
Insanely complete Ansible playbook, showing off all the options
---
# ^^^ YAML documents must begin with the document separator "---"
#
#### Example docblock, I like to put a descriptive comment at the top of my
#### playbooks.
#
# Overview: Playbook to bootstrap a new host for configuration management.
# Applies to: production
# Description:
# Ensures that a host is configured for management with Ansible.
logger = logging.getLogger(__name__)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
@litnimax
litnimax / gist:1a02be9091ace89f3603
Created October 20, 2015 14:44
Flask signal example
from flask import Flask
from flaskext.sqlalchemy import SQLAlchemy, models_comitted
app = Flask(__name__)
db = SQLAlchemy(app)
class Post(db.Model):
id = db.Column('post_id', db.Integer, primary_key=True)
title = db.Column(db.String(200))
text = db.Column(db.String)
@litnimax
litnimax / gist:4012d47a7c6f44157def
Created October 5, 2015 08:13
litnimax public key
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArmMOfgGJ8o7bBYz0s+OW/zkXRcfma/felnuxaBxr+QdILQR/u2ZaNnatIpfIZEJIpZvmnbWIcOYxjlTnHMv/uHkhIigoE85l40+Uj7On+dDiJG5/c4AGS3uX1GQFeltJaWUW3ghUHOaReYyTnsp6+htvLdPnfHJJl2XaGzLz0cWTHnBD26CBBUHU2fFDj9shWMeTc/iHKFcxlKUrDLPS1gG8/NfqDlynhh/6QMgCyaX696EFZ5QP3aAuB3umsWwv37naKLGt3lUmWtXl5Nc3xIJKZ2CIlc/Hx+/jvtuCvULt/NdWotxp9kbgJRh2G+uonRvDJK1uinohv8Z0B+C+0w== max@explorer
import json
import math
import os
import time
import webbrowser
import zmq.green as zmq
import gevent
#from geventwebsocket import WebSocketApplication
from geventwebsocket.handler import WebSocketHandler
from paste.urlparser import StaticURLParser
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
import sys
import zmq
# None of these operations will block, regardless of peer:
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.setsockopt(zmq.LINGER, 0)
socket.connect("tcp://127.0.0.1:12346")
socket.send_json({"msg": "testmsg"}) # send can block on other socket types, so keep track
# use poll for timeouts: