|
我有一个网站配置是: Django + mod-wsgi + apache,我发送另一个HTTP请求到另一个服务,并通过python的httplib库解决这个问题。但是有时候这个服务不会得到太长的响应,httplib的超时也不起作用。所以我创建了一个线程,在这个线程中我向服务发送请求,并在20秒后加入它(20秒-请求超时)。它是这样工作的:
- 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
- something like this...
- And use it by this function:
- 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')
复制代码
一切都正常运行,但有时发生这个错误
error: can't start new thread
at the line of starting new thread:
getting.start()
and the next and the final line of traceback is
File "/usr/lib/python2.5/threading.py", line 440, in start
_start_new_thread(self.__bootstrap, ())
谁能告诉我如何解决吗? |
|