Skip to content

Instantly share code, notes, and snippets.

@ajdavis
Created February 29, 2012 03:20
Show Gist options
  • Save ajdavis/1937323 to your computer and use it in GitHub Desktop.
Save ajdavis/1937323 to your computer and use it in GitHub Desktop.
Attempt to return socket to pool when thread dies
import thread
from thread import _local as local
import time
class Reaper(object):
def __init__(self, pool, sock):
self.pool = pool
self.sock = sock
def __del__(self):
print 'E.__del__'
self.pool.return_socket(self.sock)
class Pool(local):
reaper = None
def return_socket(self, sock):
print 'return_socket', sock
p = Pool()
def run():
sock = 'sock'
p.reaper = Reaper(p, sock)
print 'doing stuff in thread'
time.sleep(1)
print 'quitting thread'
thread.start_new_thread(run, ())
time.sleep(2)
print 'doing stuff on main'
time.sleep(1)
print 'doing more stuff on main'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment