Skip to content

Instantly share code, notes, and snippets.

View tchar's full-sized avatar

tchar tchar

View GitHub Profile
@tchar
tchar / service.py
Last active August 6, 2021 01:08
Singleton service sharing resources with a runner thread that supports stopping, waking up
from typing import List
from threading import Thread, RLock, Event
import time
import uuid
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@tchar
tchar / singleton.py
Created July 23, 2021 14:25
Singleton pattern, singletonmethod decorator
from typing import Any, Callable, Type
from functools import wraps
class Singleton(type):
_instances = {}
def __call__(cls: Type[Any], *args: Any, **kwargs: Any) -> Any:
'''Gets as input the class and args/kwargs and returns either an already
instantiated object or a new object from that class'''
@tchar
tchar / promisify.js
Last active February 16, 2018 18:24
Promisify a function
function promisify(func, context) {
return function(...p) {
return new Promise(function(resolve, reject) {
p.push(function(err, data) {
if (err) {
reject(err)
} else {
resolve(data)
}
})