读书人

setDaemon后的线程怎么释放资源

发布时间: 2012-03-22 17:43:57 作者: rapoo

setDaemon后的线程如何释放资源?
比如打开的数据库连:
setDamon后就只能等主线程结束了,打开的资源不受控制了。
文档中关于setDamon就要一点信息:
The entire Python program exits when no alive non-daemon threads are
left.
def thread_put(self):
con = sqlite3.connect('D:/job')
cur = con.cursor()
while True:
try:
urls,req,content = self.q_ans.get()
self.urls.extend(urls)
cur.execute("insert into http values(NULL,?,?)",
(req,content))
con.commit()
except Exception ,e:
print e,'other,excp========in=put'
finally:
self.q_ans.task_done()

[解决办法]
那你不要设成deamon,while True改一下条件,比如queue空了就退出,get加超时参数不要死等
[解决办法]

探讨

那你不要设成deamon,while True改一下条件,比如queue空了就退出,get加超时参数不要死等

[解决办法]
这个帖子里以及帖子的链接帖子和文章里提到了几种控制子线程退出的方法,你可以看看,不知道是不是你需要的:
http://stackoverflow.com/questions/2564137/python-how-to-terminate-a-thread-when-main-program-ends

读书人网 >perl python

热点推荐