Skip to content

Instantly share code, notes, and snippets.

View pavelpatrin's full-sized avatar

Pavel Patrin pavelpatrin

  • Yerevan, Armenia
View GitHub Profile
@pavelpatrin
pavelpatrin / Atomic.go
Last active January 26, 2021 19:47
Example of atomic store
package main
import (
"fmt"
"math/bits"
"runtime"
"sync/atomic"
"time"
)
@pavelpatrin
pavelpatrin / gist:5bc22ccec50d73b348cf0fa2b5690fa9
Created January 1, 2021 13:18
Multi-thread access to single generator
import logging
import threading
import time
import typing
def generator():
time.sleep(5)
yield 123
return 456
@pavelpatrin
pavelpatrin / packetizer.py
Last active July 25, 2019 20:27
Pip to Rpm packetizer
import argparse
import codecs
import dataclasses
import json
import logging
import os
import shutil
import subprocess
import typing
import re
@pavelpatrin
pavelpatrin / Sentry-Mysql-Redis-Docker.txt
Last active October 22, 2018 21:27
Sentry bootstrap (MySQL, Redis, Docker, Systemd)
[root@li145-5 Sentry]# cat Dockerfile
FROM centos/systemd
CMD ["/usr/sbin/init"]
RUN yum install -y epel-release
RUN yum install -y redis
RUN systemctl enable redis
@pavelpatrin
pavelpatrin / Stupid-Python-Multiprocessing.py
Last active October 22, 2018 21:30
Stupid multiprocessing
# coding: utf-8
import multiprocessing
import subprocess
import threading
import signal
import time
import os
@pavelpatrin
pavelpatrin / Line-Profiler-Decorator.py
Last active October 22, 2018 21:31
Line profiler decorator
def profile(func):
from line_profiler import LineProfiler
def wrapper(*args, **kwargs):
profiler = LineProfiler(func)
try:
return profiler.runcall(func, *args, **kwargs)
finally:
profiler.print_stats()
@pavelpatrin
pavelpatrin / Flame-Graph-Middleware.py
Last active October 22, 2018 21:32
Flamegraph middleware that tracks requests
class FlamegraphMiddleware(object):
def __init__(self):
import os, socket, datetime
arguments = os.getpid(), socket.gethostname()
filename = '/tmp/flamegraph.%s.%s.log' % arguments
self.profiler = self.create_profiler(filename)
print('FlameGraph started: %s' % filename)
def __del__(self):
def matrix(side_width):
for y in xrange(side_width):
for x in xrange(side_width):
ring_number = min(x, y, side_width - 1 - x, side_width - 1 - y)
ring_serial = x + y - 2 * ring_number
if x >= y:
ring_base = 4 * ring_number * (side_width - ring_number)
result = ring_base + ring_serial
else:
class RawJoin(Join):
u"""Позволяет внедрить в QuerySet произвольный JOIN.
banners_qs = Banner.objects.extra(where=['my_join_table.status = 123'])
Join('LEFT JOIN (XXX) b1 ON b1.id = abcd ', ()).inject(banners_qs)
"""
def __init__(self, raw_sql, params):
# Используемые опции.
import select
import socket
from urllib.parse import urlsplit
class Poll:
ACTION_READ = 'read'
ACTION_WRITE = 'write'