Skip to content

Instantly share code, notes, and snippets.

View h0rn3t's full-sized avatar
🏠
Working from home

Eugene Shershen h0rn3t

🏠
Working from home
  • Ukrenergo Digital Solutions
  • Ukraine
View GitHub Profile
@h0rn3t
h0rn3t / background_tasks.py
Created May 22, 2016 18:29 — forked from blakev/background_tasks.py
Background tasks manager for gevent Greenlets
#!/usr/bin/env python3
# ~*~ coding: utf-8 ~*~
#
# >>
# .. created: 5/20/16
# .. author: blake.vandemerwe
#
# LICENSE
# <<
@h0rn3t
h0rn3t / concurrent.futures-intro.md
Created April 15, 2016 21:27 — forked from mangecoeur/concurrent.futures-intro.md
Easy parallel python with concurrent.futures

Easy parallel python with concurrent.futures

As of version 3.3, python includes the very promising concurrent.futures module, with elegant context managers for running tasks concurrently. Thanks to the simple and consistent interface you can use both threads and processes with minimal effort.

For most CPU bound tasks - anything that is heavy number crunching - you want your program to use all the CPUs in your PC. The simplest way to get a CPU bound task to run in parallel is to use the ProcessPoolExecutor, which will create enough sub-processes to keep all your CPUs busy.

We use the context manager thusly:

with concurrent.futures.ProcessPoolExecutor() as executor:
@h0rn3t
h0rn3t / async_await_sample.py
Created April 14, 2016 11:39
Sample code demonstrating use of await with concurrent.futures
from concurrent.futures import ThreadPoolExecutor, Future
import asyncio
import time
async def sum_numbers(x, y):
await asyncio.sleep(1)
return x + y
def sum_numbers_blocking(x, y):
time.sleep(1)
@h0rn3t
h0rn3t / threadpool.py
Created April 14, 2016 11:36 — forked from andreisavu/threadpool.py
Python Thread Pool
# Original code at http://code.activestate.com/recipes/203871/
# I have added a limit for the number of tasks waiting
# The method queueTask will block if the current taskList size
# exceeds the specified limit
import threading
from time import sleep
# Ensure booleans exist (not needed for Python 2.2.1 or higher)
#---------------------------------------------
# TINT2 CONFIG FILE
#---------------------------------------------
# For more information about tint2, see:
# http://code.google.com/p/tint2/wiki/Welcome
#
# For more config file examples, see:
# http://bunsenlabs.org/topic/3232/my-tint2-config/
# Background definitions
@h0rn3t
h0rn3t / gist:cb57364889874e2fac96
Created February 12, 2016 14:14
multiprocessing gevent
import sys
from gevent import server
from gevent.monkey import patch_all; patch_all()
from multiprocessing import Process, current_process, cpu_count
def note(format, *args):
sys.stderr.write('[%s]\t%s\n' % (current_process().name, format%args))
def echo(socket, address):
@h0rn3t
h0rn3t / gist:c51f41e210fbc753941b
Created February 12, 2016 13:38
gevent multicore WSGIServer
import sys
from gevent import server
from gevent.baseserver import _tcp_listener
from gevent import pywsgi
from gevent.monkey import patch_all; patch_all()
from multiprocessing import Process, current_process, cpu_count
def hello_world(env, start_response):
if env['PATH_INFO'] == '/':
start_response('200 OK', [('Content-Type', 'text/html')])
@h0rn3t
h0rn3t / gist:d10bff33dc10ddb20e78
Created January 18, 2016 10:40
MONGODB & PYTHON
###Ubuntu Install
```
sudo apt-get install mongodb
pip install pymongo
```
Table - Collection
Column - Property
Row - Document
@h0rn3t
h0rn3t / gist:269f50c622d4132ef267
Created September 9, 2015 07:06
mysql db wrapper
#!/usr/bin/env python
################################################################
# simpledb: a simple mysql db wrapper module with auto connect
#
# Author: Curu Wong
# Date: 2014-08-25
# License: GPL v3
##########################################################
import sys,time
import MySQLdb
@h0rn3t
h0rn3t / gist:c0e7bda9e390f44a2e4c
Created July 12, 2015 08:24
fluent decorator
from functools import wraps
from copy import deepcopy
def fluent(method):
"""Used to define fluent API class methods"""
@wraps(method)
def wrapped(self, *args, **kwargs):
dupe = deepcopy(self)
method(dupe, *args, **kwargs)
return dupe