Tengo un sitio que se ejecuta con la configuración siguiente:error: No se puede iniciar un nuevo hilo
Django + mod-WSGI + Apache
En uno de petición del usuario, que enviar otra solicitud HTTP a otro servicio y resuelve esto mediante la biblioteca httplib de python.
Pero a veces este servicio no recibe una respuesta demasiado larga, y el tiempo de espera para httplib no funciona. Así que estoy creando un hilo, en este hilo envío una solicitud al servicio, y me uno a él después de 20 segundos (20 segundos - es un tiempo de espera de solicitud). Así es como funciona:
class HttpGetTimeOut(threading.Thread):
def __init__(self,**kwargs):
self.config = kwargs
self.resp_data = None
self.exception = None
super(HttpGetTimeOut,self).__init__()
def run(self):
h = httplib.HTTPSConnection(self.config['server'])
h.connect()
sended_data = self.config['sended_data']
h.putrequest("POST", self.config['path'])
h.putheader("Content-Length", str(len(sended_data)))
h.putheader("Content-Type", 'text/xml; charset="utf-8"')
if 'base_auth' in self.config:
base64string = base64.encodestring('%s:%s' % self.config['base_auth'])[:-1]
h.putheader("Authorization", "Basic %s" % base64string)
h.endheaders()
try:
h.send(sended_data)
self.resp_data = h.getresponse()
except httplib.HTTPException,e:
self.exception = e
except Exception,e:
self.exception = e
algo como esto ...
y usarlo por esta función:
getting = HttpGetTimeOut(**req_config)
getting.start()
getting.join(COOPERATION_TIMEOUT)
if getting.isAlive(): #maybe need some block
getting._Thread__stop()
raise ValueError('Timeout')
else:
if getting.resp_data:
r = getting.resp_data
else:
if getting.exception:
raise ValueError('REquest Exception')
else:
raise ValueError('Undefined exception')
Y todo funciona bien, pero en algún momento que se inicia la captura de esta excepción:
error: can't start new thread
en la línea de partida nuevo hilo:
getting.start()
y la siguiente y la última línea de rastreo es
File "/usr/lib/python2.5/threading.py", line 440, in start
_start_new_thread(self.__bootstrap,())
Y la respuesta es: ¿Cuál es suceda?
Gracias a todos, y perdón por mi inglés puro. :)
Tenga en cuenta que la cantidad de subprocesos en ejecución se puede mostrar con 'threading.active_count()'. – 101
Sugerencia útil, ¡gracias! –