multi-threading patch

This commit is contained in:
Miroslav Stampar 2010-06-01 18:40:34 +00:00
parent eb94edc48c
commit 4d6d5c8447

View File

@ -68,6 +68,7 @@ EXTRA ATTRIBUTES AND METHODS
""" """
import threading
import urllib2 import urllib2
import httplib import httplib
import socket import socket
@ -89,19 +90,28 @@ class HTTPHandler(urllib2.HTTPHandler):
def open_connections(self): def open_connections(self):
"""return a list of connected hosts""" """return a list of connected hosts"""
return self._connections.keys() retVal = []
currentThread = threading.currentThread()
for thread, host in self._connections.keys():
if thread == currentThread:
retVal.append(host)
return retVal
def close_all(self): def close_all(self):
"""close all open connections""" """close all open connections"""
for host, conn in self._connections.items(): for _, conn in self._connections.items():
conn.close() conn.close()
self._connections = {} self._connections = {}
def _remove_connection(self, host, close=0): def _remove_connection(self, host, close=0):
if self._connections.has_key(host): key = self._get_connection_key(host)
if close: self._connections[host].close() if self._connections.has_key(key):
del self._connections[host] if close: self._connections[key].close()
del self._connections[key]
def _get_connection_key(self, host):
return (threading.currentThread(), host)
def _start_connection(self, h, req): def _start_connection(self, h, req):
try: try:
if req.has_data(): if req.has_data():
@ -132,7 +142,8 @@ class HTTPHandler(urllib2.HTTPHandler):
try: try:
need_new_connection = 1 need_new_connection = 1
h = self._connections.get(host) key = self._get_connection_key(host)
h = self._connections.get(key)
if not h is None: if not h is None:
try: try:
self._start_connection(h, req) self._start_connection(h, req)
@ -155,7 +166,7 @@ class HTTPHandler(urllib2.HTTPHandler):
if need_new_connection: if need_new_connection:
if DEBUG: print "creating new connection to %s" % host if DEBUG: print "creating new connection to %s" % host
h = http_class(host) h = http_class(host)
self._connections[host] = h self._connections[key] = h
self._start_connection(h, req) self._start_connection(h, req)
r = h.getresponse() r = h.getresponse()
except socket.error, err: except socket.error, err: