This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from functools import wraps | |
def waitpid(func): | |
cache = {} | |
@wraps(func) | |
def wrapper(pid, options): | |
try: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Process | |
import time | |
import signal | |
import os | |
import psutil | |
def start(): | |
raise Exception('BOOM!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PoolManager: # already existing class | |
def start(self): | |
self.init_signals() # added by me | |
self.worker_manager.create_workers() | |
# all below added by me | |
def init_signals(self): | |
if hasattr(signal, 'SIGCHLD'): | |
signal.signal(signal.SIGCHLD, handle_chld) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def setblocking(): | |
setblocking_func = socket.socket.setblocking | |
def wrapper(self, flag): | |
if flag: | |
# prohibit timeout reset | |
timeout = socket.getdefaulttimeout() | |
if timeout: | |
self.settimeout(timeout) | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
def settimeout(): | |
settimeout_func = socket.socket.settimeout | |
def wrapper(self, timeout): | |
# prohibit timeout reset | |
if timeout is None: | |
timeout = socket.getdefaulttimeout() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import weakref | |
class MyResource: | |
def __del__(self): | |
print('resource is deleted') | |
class MyWeakKeyDictionary(weakref.WeakKeyDictionary): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import weakref | |
class MyResource: | |
def __del__(self): | |
print('resource is deleted') | |
class MyWeakKeyDictionary(weakref.WeakKeyDictionary): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import weakref | |
from some_where import MyServer | |
class MyWeakKeyDictionary(weakref.WeakKeyDictionary): | |
def __init__(self, *args, **kwgs): | |
super().__init__(*args, **kwgs) | |
remove = self._remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _check_workers(self): | |
workers = [] | |
for worker in self._workers: | |
if worker.is_alive(): # worker is python thread instance | |
workers.append(worker) | |
else: # don't keep died worker in pool, reducing its references count | |
new_worker = self._get_worker() | |
new_worker.start() | |
workers.append(new_worker) | |
self._workers = workers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import weakref | |
import boto3 | |
import boto3.session | |
class SQS: | |
def __init__(self, | |
queue_name, |