错误:无法启动新线程 [英] error: can't start new thread

查看:412
本文介绍了错误:无法启动新线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站运行,遵循配置:

I have a site that runs with follow configuration:

Django + mod-wsgi + apache

Django + mod-wsgi + apache

在用户的请求之一中,我向另一个服务发送另一个HTTP请求,并通过python的httplib库解决。

In one of user's request, I send another HTTP request to another service, and solve this by httplib library of python.

但是有时这个服务没有得到答案太长,httplib的超时不起作用。所以我创建线程,在这个线程我发送请求服务,并加入它20秒后(20秒 - 是超时请求)。这是它的工作原理:

But sometimes this service don't get answer too long, and timeout for httplib doesn't work. So I creating thread, in this thread I send request to service, and join it after 20 sec (20 sec - is a timeout of request). This is how it works:

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

这样的东西...

并将其用于功能:

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')

所有功能都可以正常工作,但有时我开始捕获这个异常: / p>

And all works fine, but sometime I start catching this exception:

error: can't start new thread

在行o f开始新线程:

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, ())

答案是:发生了什么?

感谢所有人,对我的纯英语感到抱歉。 :)

Thank's for all, and sorry for my pure English. :)

推荐答案

无法启动新线程错误几乎肯定是由于您已经有太多的事实线程运行在您的python进程中,并且由于某种资源限制,创建新线程的请求被拒绝。

The "can't start new thread" error almost certainly due to the fact that you have already have too many threads running within your python process, and due to a resource limit of some kind the request to create a new thread is refused.

你应该看看线程数你正在创造;您将能够创建的最大数量将由您的环境确定,但至少应该在数百的数量之内。

You should probably look at the number of threads you're creating; the maximum number you will be able to create will be determined by your environment, but it should be in the order of hundreds at least.

这可能是一个好主意在这里重新思考你的建筑;看到这是异步运行无论如何,也许你可以使用一个线程池从另一个网站获取资源,而不是总是为每个请求启动一个线程。

It would probably be a good idea to re-think your architecture here; seeing as this is running asynchronously anyhow, perhaps you could use a pool of threads to fetch resources from another site instead of always starting up a thread for every request.

另一个改进要考虑的是你使用Thread.join和Thread.stop;这可能会通过为HTTPSConnection的构造函数提供一个超时值来实现。

Another improvement to consider is your use of Thread.join and Thread.stop; this would probably be better accomplished by providing a timeout value to the constructor of HTTPSConnection.

这篇关于错误:无法启动新线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆