Skip to content

Instantly share code, notes, and snippets.

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

Eugene h0rn3t

🏠
Working from home
  • Ukraine
View GitHub Profile
@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)
@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 / 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 / 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
# <<
#!/usr/bin/env python3
# coding: utf-8
from db import db
import time
import calendar
month = 43200*60
day = 24*60*60
week = 24*60*60*7
@h0rn3t
h0rn3t / echo.go
Created May 10, 2017 18:26 — forked from paulsmith/echo.go
A simple echo server testing a few interesting Go language features, goroutines and channels.
// $ 6g echo.go && 6l -o echo echo.6
// $ ./echo
//
// ~ in another terminal ~
//
// $ nc localhost 3540
package main
import (
@h0rn3t
h0rn3t / aes-ecb.py
Last active April 15, 2025 12:40 — forked from lopes/aes-ecb.py
Simple Python example of AES in ECB mode.
from hashlib import md5
from base64 import b64decode
from base64 import b64encode
from Crypto.Cipher import AES
# Padding for the input string --not
# related to encryption itself.
BLOCK_SIZE = 16 # Bytes
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * \
@h0rn3t
h0rn3t / gist:62d07174e213d40dc82fb07742a2f73d
Created May 2, 2018 08:55
Freezes fix after Sierra few minutes idle
sudo rm /System/Library/LaunchAgents/com.apple.mediaanalysisd.plist
@h0rn3t
h0rn3t / haproxy, sending the source ip to the webserver.
Created September 17, 2018 08:45 — forked from PiBa-NL/haproxy, sending the source ip to the webserver.
haproxy, sending the source ip to the webserver.
To send the ip addres of the client/webbrowser to the server/webserver behind it there are a few options:
1- option forwardfor
2- send-proxy
3- source 0.0.0.0 usesrc clientip
1- option forwardfor
This is an easy option to configure in haproxy, it does require that http layer7 processing is used 'mode http' and the webserver/ webapplication that wants to log or use the ip of the client must use the http-header 'X-Forwarded-For' to read the clientip.
2- send-proxy / send-proxy-v2 / send-proxy-*
This is can be used both with mode tcp and http, it does however require that the server also understands the proxyprotocol. Some applications have added support for this protocol which adds a few bytes with ip information before the actual request.
@h0rn3t
h0rn3t / mongodb_model.py
Created October 17, 2018 08:59 — forked from fatiherikli/mongodb_model.py
Very simple MongoDB Model in Python
from pymongo import Connection
from bson import ObjectId
from itertools import imap
class Model(dict):
"""
A simple model that wraps mongodb document
"""
__getattr__ = dict.get